A more efficient way of writing an if then Structure

I have an integer variable named MyVariable in a Visual Basic Project. I also have three Boolean Variables named Bool1, Bool2 and Bool3 in my project. I have an If Then statement structure like the following:

  If MyVariable = 1 Then
            MsgBox("Hello")
            Bool1 = True

        ElseIf MyVariable = 2 Then
            MsgBox("Hello")
            Bool2 = True

        ElseIf MyVariable = 3 Then
            MsgBox("Hello")
            Bool3 = True
  End If

I am wondering if there is a more efficient way of writing this if then structure. Thank you for your help in anticipation.
LVL 1
FaheemAhmadGulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PryratesCommented:
you can use a select case statement
http://www.homeandlearn.co.uk/net/nets1p21.html

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
als315Commented:
Or you can use array (BoolA, for example) for boolean variables.
In this case you will need only string
BoolA(MyVariable) = true
cyberkiwiCommented:
Because 6 variables are involved in 3 branches, there really isn't a better way.
You could do the hello a better way, but not with also setting the boolean variables.

  If MyVariable = 1 Then
            Bool1 = True
        ElseIf MyVariable = 2 Then
            Bool2 = True
        ElseIf MyVariable = 3 Then
            Bool3 = True
  End If

If Bool1 or Bool2 or Bool3 Then ' assuming they were all previously false
            MsgBox("Hello")
End If
FaheemAhmadGulAuthor Commented:
Many thanks. As all three comments were helpful, I am splitting the points and accepting all as correct. Regards
GrahamSkanRetiredCommented:
Just to add that your example can be written quite neatly using Select Case
Select Case MyVariable
   Case 1, 2, 3
        MsgBox ("Hello")
        Bool1 = True
End If

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.