Link to home
Create AccountLog in
Avatar of FaheemAhmadGul
FaheemAhmadGulFlag for United Kingdom of Great Britain and Northern Ireland

asked on

A more succinct way of writing an If Then Structure

In Visual Basic Project I have an integer variable named myVariable which can have a value from 1 to 3 and I have five Boolean Variables named YesNo1, YesNo2, YesNo3, YesNo4, YesNo5. I have an If then Structure like the following:

        If myVariable = 1 Then
            YesNo01 = True
            YesNo02 = False
            YesNo03 = False
            YesNo04 = False
            YesNo05 = False



        ElseIf myVariable = 2 Then
            YesNo01 = False
            YesNo02 = True
            YesNo03 = False
            YesNo04 = False
            YesNo05 = False


        ElseIf myVariable = 3 Then
            YesNo01 = False
            YesNo02 = False
            YesNo03 = True
            YesNo04 = False
            YesNo05 = False




        End If

As you will notice in this a YesNo Variable corresponding to the myVariable becomes True and all the remaining become false.
I wish to know if there is more efficient way of writing the above structure. In my actual project myVariable can have a value from 1 to 25 and there are some 25 YesNo kind of variables and therefore it would help me if could find a more succinct way of writing the above structure.

Thank you for your help in anticipation.
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of FaheemAhmadGul

ASKER

Thank you for your comment. I am a beginner (not fully aware of how to declare and use arrays) and do not know how to implement the solution you have suggested. I would be grateful if you could provide a an  example of how to do this for the problem I have posted. Thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Many thanks. I am splitting the points with more points for Graham as the solution posted by him was more complete and really helped me understand the concept.