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

I am trying to get some code working (related to the VB mess I winged about!)
In C# I have used enums as a way of deciding if a piece of hardware has responded.
So I am now trying to get a version of my code in C# to function in VB, in C# I have three enums set up
NO_REPLY for Not replied
TIMEOUT_REPLY for a reply that has not arrived in the time out period set by a timer
YES_REPLY for a reply has been received

In the read function you set Reply_Status to be equal to No
write the data out & start the timer the code below is the VB translation which is not working, however if I (as can be seen) stick a message box in it prints up 0 for no reply
and gets the data once but never again.

VB
Reply_Status = REPLY.NO_REPLY
       myComPort.Write(Data_Send)
       MsgBox(Reply_Status)
       While (Reply_Status = REPLY.NO_REPLY)
           NoDataAtPort.Enabled = True
       End While

Can the Enums in VB be used this way?
Glenn
Posted

They can, but it depends a little bit on how and where you set up the Reply_Status variable, and on what value you expect to see from the MsgBox.

Obviously Reply_Status should be determined outside the method where you have written this While loop, so that its value can be changed by another thread. Just to make sure it is best to explicitly determine it as type REPLY (also the REPLY enum should be public), so
VB
Dim Reply_Status As REPLY
or
VB
Public Reply_Status As REPLY
or something like that.

In your example code the MsgBox will return the integer value of the Reply_Status variable.

Regards,
Johan
 
Share this answer
 
Comments
glennPattonWork3 5-Feb-14 6:05am    
Ahh, that makes some sense!
glennPattonWork3 5-Feb-14 6:07am    
How ever on testing it, it only works once if the message box is enabled...
Johan Hakkesteegt 5-Feb-14 7:24am    
Sorry, I am not sure that I understand what you mean...

The MsgBox is outside the While loop, so therefore it will obviously only be triggered once. Specifically just before the code starts the While loop.

Then in the loop the NoDataAtPort.Enabled is set to true, and you did not show any code that might set it back to false, so for all intents and purposes that also only happens once...?
glennPattonWork3 5-Feb-14 7:38am    
The message box was put there to see what state the enum was. When run with the message box it produced the message that was sent (this is a Serial Comms program) if the message box is enabled it allows the message to be received once (and only once) it locks the UI. I have experimented with Pauses in and around the while loops to no effect!
Johan Hakkesteegt 5-Feb-14 7:50am    
Ok, I see. However you show that information to the user, you need to do it on a different thread than the While loop.

So basically, try and keep the MsgBox on the GUI threaad, and the While loop on its own thread.
Quote:
Apologies if it's a stupid response. I think it can be used this way. But shouldn't it be [While (Reply_Status == REPLY.NO_REPLY)] (double equals)

--------------------------------------------------------------------------------

To CoderPanda if you can read this, got an Email saying you replied with above '==' is a C (ism) for comparing (I did that first) but VB put the blue wiggly line of doom below it! and said it didn't make sense(mutter mutter).
 
Share this answer
 
Comments
CoderPanda 5-Feb-14 6:07am    
yes, it was indeed a stupid response from me. I overlooked you clearly mentioned VB.net.
glennPattonWork3 5-Feb-14 6:09am    
No worries, I'm really a C guy too! I hate this VB malarkey!!
Johan Hakkesteegt 5-Feb-14 7:45am    
The trick to VB malarkey, is to understand that VB syntax is implicit in a different way than C#. It makes VB slightly quicker and easier to write code in, and it makes it slightly less flexible, and it can indeed make it confusing to read when in a C# state of mind.

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



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