Select Case help please

John Cassell

Well-known member
Joined
Mar 20, 2007
Messages
65
Programming Experience
Beginner
Hi There,

I am just getting my head around using Select Case instead of multiple IF statements but I am struggling to find a way around this one..

For simple purposes, I have two text boxes on my form txtbox1 and txtbox2.

I need for the code to say "If txtbox1 or txtbox2 are blank then tell me to put something in both boxes otherwise tell me something happened that I wasn't expecting.."

Here is my code:

VB.NET:
 Select Case txtbox1.Text
            Case "" or txtbox2.Text = ""
                MsgBox("Please enter something in BOTH boxes.")
            Case Else
                MsgBox("I'm sorry something has happened which I don't have an answer for!")
        End Select

but this gives me the error 'Conversion from string "" to type 'Boolean' is not valid.'

can anyone help please?

Thanks

John
 
You simply wouldn't use a Select Case in that situation. A Select Case is for when you want to test one variable against multiple possible values. You want to test two variables, so Select Case is not appropriate. You simply need an If statement:
VB.NET:
If txtbox1.Text = String.Empty OrElse txtbox2.Text = String.Empty Then
 
thanks for the reply. OK, thats fine then just thought there may be a way.

Just another thing if you wouldn't mind.. I always use ( If txtbox.text = "" ) to find out if the box is empty but I notice you use string.empty instead. To me that seems more long winded. I'm sure there is a good reason for this but in my efforts to learn I would appreciate to know what it is.

Thanks again for the original reply, really appreciated.

John
 
It is preferred to use the String.Empty field because it is a single, existing String object, rather than creating a new object every time you want an empty string.
 
Back
Top