Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi All,

I am trying to replicate a an 'if, then' block in VB that I wrote in C# (That is in my Article Serial Comms in C# for Beginners), the block in C# is:

C#
private string Write_ATE(string Data_To_ATE)
{
    string Data_From_ATE = "";
    Reply_Status = (int)REPLY.NO_REPLY;
    ATEComPort.Write(Data_To_ATE);
    while (Reply_Status == (int)REPLY.NO_REPLY)
    {
        NoDataAtPort.Enabled = true;
    }
    NoDataAtPort.Enabled = false;
    if (Reply_Status == (int)REPLY.TIMEOUT_REPLY)
    {
         Reply_Status = (int)REPLY.NO_REPLY;
        ATEComPort.Write(Data_To_ATE);
        while (Reply_Status == (int)REPLY.NO_REPLY)
        {
            NoDataAtPort.Enabled = true;
        }
        NoDataAtPort.Enabled = false;
        if (Reply_Status == (int)REPLY.TIMEOUT_REPLY)
        {
                    Data_From_ATE = "TIMEOUT";
            return (Data_From_ATE);
        }
        else if (Reply_Status == (int)REPLY.YES_REPLY)
        {
            Data_From_ATE = ATEComPort.ReadTo("\r\n");
           if ((Data_From_ATE.Substring(0, 1)) == (Data_To_ATE.Substring(1, 1)))
           {
             return (Data_From_ATE);
           }
        }
        else
        {
           Data_From_ATE = "SERIOUS_ERROR";
           return (Data_From_ATE);
       }
     }
     else if (Reply_Status == (int)REPLY.YES_REPLY)
     {
       Data_From_ATE = ATEComPort.ReadTo("\r\n");
       if ((Data_From_ATE.Substring(0, 1)) == (Data_To_ATE.Substring(1, 1)))
       {
         return (Data_From_ATE);
       }


I say it in the Article I'll say it again here, "it's ugly but it seems to work!"
which through some swearing is some more ElseIf I think, I have tried to use the developer fusion tool with out success:


VB
Dim a As Integer
     Data_Back = ""
     Reply_Status = REPLY.NO_REPLY

     TestComPort.Write(Data_Send)
     While (Reply_Status = REPLY.NO_REPLY)
         TmrNoDataAtPort.Enabled = True
     End While
     TmrNoDataAtPort.Enabled = False
     If (Reply_Status = REPLY.TIMEOUT_REPLY) Then
         TestComPort.Write(Data_Send)
     Else
         While (Reply_Status = REPLY.NO_REPLY)
             TmrNoDataAtPort.Enabled = True
         End While
         TmrNoDataAtPort.Enabled = False
         Else If (Reply_Status = REPLY.TIMEOUT_REPLY) Then
         Data_Back = "TIMEOUT"

     Else if
         If (Reply_Status = REPLY.YES_REPLY) Then
             Data_Back = TestComPort.ReadTo(Chr(10))
             rtbIncoming.Text += Data_Back
         End If
     End If


The thing is I do not know how to do a multiple if then statement as you would do in C
such as
C++
if(blah = blah)
{
do_stuff
}else if(blah = blah2)
{
do_something_else
}else if(blah = blah3)
{
do_something_else_again
}
in VB is this
VB
if(blah = blah)then
do_stuff
else if (blah = blah2) then
do_something_stuff
else if (blah = blah3) then 
do_something_else
end if 

Cannot tell if this would work trying again!
Hmmm..., I have this
VB
Dim a As Integer
Data_Back = ""
Reply_Status = REPLY.NO_REPLY

TestComPort.Write(Data_Send)
While (Reply_Status = REPLY.NO_REPLY)
    TmrNoDataAtPort.Enabled = True
End While
TmrNoDataAtPort.Enabled = False
If (Reply_Status = REPLY.TIMEOUT_REPLY) Then
    TestComPort.Write(Data_Send)
Elseif
    While (Reply_Status = REPLY.NO_REPLY)
        TmrNoDataAtPort.Enabled = True
    End While
    TmrNoDataAtPort.Enabled = False
ElseIf (Reply_Status = REPLY.TIMEOUT_REPLY) Then
    Data_Back = "TIMEOUT"

ElseIf (Reply_Status = REPLY.YES_REPLY) Then
    Data_Back = TestComPort.ReadTo(Chr(10))
    rtbIncoming.Text += Data_Back
End If


it appears that the Elseif after the If(Reply_Status = REPLY.TIMEOUT) that is breaking things...


Glenn
Posted
Updated 28-Feb-14 5:13am
v3
Comments
Sergey Alexandrovich Kryukov 28-Feb-14 11:13am    
And what's not working? If the behavior of your code does not match what you expected, use the debugger and fix it. If you failed to do so, describe what did you want to achieve, what's expected behavior, what is observed behavior, why do you feel it's wrong. If you have some exceptions, describe steps to reproduce, complete exception information. Comment the points in code related to exception throwing/propagation. Post all what's relevant. See also: http://www.sscce.org.

—SA
glennPattonWork3 28-Feb-14 11:24am    
Thanks...
Dave Kreskowiak 28-Feb-14 18:34pm    
Odd. I don't see a single line of code that has anything to do with a timer.

1 solution

Did you notice blah = blah always returns true, which makes all other branched of your if unreachable?
—SA
 
Share this answer
 
Comments
glennPattonWork3 28-Feb-14 11:21am    
Now you mention it should == (the Equality Operator not the Assignment operator)
Sergey Alexandrovich Kryukov 28-Feb-14 11:24am    
It depends on the language and context. In your code, under "if", this is comparison, but left and right are the same. How it can be unclear?
If it was assignment, it was a statement doing nothing.
—SA
glennPattonWork3 28-Feb-14 11:28am    
Really it was thinking while typing
Sergey Alexandrovich Kryukov 28-Feb-14 11:39am    
What do you mean, your question?
—SA
glennPattonWork3 28-Feb-14 11:41am    
Yup! I have an example of what I am trying to do in VB now:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900