Avatar of FaheemAhmadGul
FaheemAhmadGulFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

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


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

 
.NET ProgrammingVisual Basic Classic

Avatar of undefined
Last Comment
FaheemAhmadGul
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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
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?
Avatar of FaheemAhmadGul

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of FaheemAhmadGul

ASKER

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.
Avatar of FaheemAhmadGul

ASKER

Many thanks. It was brilliant ! !
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo