Link to home
Start Free TrialLog in
Avatar of FaheemAhmadGul
FaheemAhmadGulFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Pryrates
Pryrates
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FaheemAhmadGul

ASKER

Many thanks. As all three comments were helpful, I am splitting the points and accepting all as correct. Regards
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