How to pass the name of a Bolean variable in a String Variable and setting the Boolean Variable True Using the String Variable.

FaheemAhmadGul used Ask the Experts™

I have a Global Boolean variable in my project in Visual Basic. named    YesNo01
I wish to make it True through the Click event of a Button . However, instead of setting it True directly by using this Boolean Variable’s own name, I wish to set it True using a String Variable that is carrying its name.
So what I am looking for is something like this:

1.      Dim YesNo01 As Boolean
2.      Dim myString as String
3.      myString = “YesNo01”
4.      myString = True
I had hoped that the 4th line of code will make YesNo01 = True, but this does not work.
I wish to know if it is possible to achieve something like this. That is passing the name of one Boolean Variable in a String Variable and then setting the orginal Boolean variable true using that string variable. I hope I have been able to clarify my question.
Thank you for your help.

Best wishes

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012

I don't think that this is possible. MInd you, whenever I say that, someone usually comes and contradicts me.

There is an AddressOf() operator, but it only refers to functions.

You could consider using an array of variables, with named constants to help with readability, perhaps using an Enum class

Dim YesNo(2) as Boolean

Enum MyBool (YesNo00, YesNo01, YesNo02)

YesNo(YesNo01) = true
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Is this VB6 or VB.Net?

How are you making the variable global?  Is it a Public member in a Module?...or a Shared member in a Class?


Thank you for your comments experts. My apologies for the delay in responding to your comments.

This is VB.NET. I have declared this in Module1 of my project as follows:
Public YesNo01 As Boolean
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
This example shows how to both retrieve and set the value of the variable via Reflection:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim VariableName As String = "YesNo01"
        Dim x As System.Reflection.FieldInfo = GetType(Module1).GetField(VariableName, Reflection.BindingFlags.Static Or Reflection.BindingFlags.Public)
        If Not IsNothing(x) Then
            MessageBox.Show(VariableName & " = " & x.GetValue(Nothing))

            x.SetValue(Nothing, True)

            MessageBox.Show(VariableName & " = " & x.GetValue(Nothing))
        End If

    End Sub

End Class

Public Module Module1

    Public YesNo01 As Boolean = False

End Module

Open in new window


Exceptional ! ! !

As this was a far more difficult question than I originally thought I am increasing the points to 400 before accepting Idle_Mind's solution.
Dear Idle_Mind System Reflection seems to be a very helpful yet difficult concept. I wonder if you would consider writing an article in EE sometime explaining how to use it in difficult scenarios.
Many thanks.


Many thanks. It was brilliant ! !

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