How to pass the name of a Bolean variable in a String Variable and then Use that String Variable as a Replacement for the Name of the Boolean Variable in Code

FaheemAhmadGul
FaheemAhmadGul used Ask the Experts™
on
This question is related to another question I asked on the Forum today and which was very kindly answered by Idle_Mind.

I have a Global Boolean variable in my project in a Visual Basic  .NET Project  named    YesNo01. This YesNo01 is declared in a Module1 of my Project.

I wish to write some code along the following lines
1. If  YesNo01 = True Then
2. MessageBox.Show (“YesNo01 = True”)
3. Else
3. MessageBox.Show (“YesNo01 = False”)
4. End if

However  I do not want to use the actual Name of  YesNo01  in statement No 1. Instead I wish to say something like the following:

Dim myString as String
myString = “YesNo01”
If myString = True Then
MessageBox.Show (“YesNo01 = True”)
Else
MessageBox.Show (“YesNo01 = False”)
End if

Thank you for your help in anticipation.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
You can use a Dictionary:
Dim variables As New System.Collections.Generic.Dictionary(Of String, Boolean)

...

variables.Add("YesNo01", True)

...

Dim myString as String

myString = "YesNo01"

If variables(myString) = True Then
    MessageBox.Show ("YesNo01 = True")
Else
    MessageBox.Show ("YesNo01 = False")
End if

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
The most ~direct~ route would be using a Dictionary as kaufmed suggests.  The Reflection approach I posted in your last question can't really be shortened.  It can be placed into a function I suppose...

How are you going to use this?  What is the "big picture"?

Author

Commented:
Excellent many thanks.
Idle_MInd the big picture is hard to explain briefly, but in my code somewhere I need to check if one of the several YesNo01 variables is true before the rest of the code runs. I cannot tell the code directly in advance which of the several YesNo variables (YesNo1, YesNo02 etc ) my code should check the value of before running further, but I can provide my code the value of YesNo variable it should check through a string variable. Hence these two questions related in theme. Many thanks both of you for your kind help.
Regards
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
NP. Glad to help  : )

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial