Solved

call or set variable value with variable name stored in string

Posted on 2010-09-03
5
396 Views
Last Modified: 2012-05-10
Hi,

I'm looking to call or set the value of a variable with the variable name stored as a string.

for example,

Dim value as String = "12345"
Dim VarName as String = "value"

Messagebox.show( someprocedure(VarName) )

' in this case i world be looking a message box containing: 12345

this examble does not reflect what i need this function for... I need to be able to loop though
multiple values and call / set there values.

Any help would be appresiated.

Thanks,

Tommy
0
Comment
Question by:_Tommy_b
5 Comments
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 33594806
Remove double quote near value and try.

Dim VarName as String = value

Open in new window

0
 

Author Comment

by:_Tommy_b
ID: 33594859
Hello rajapandian,

Sorry perhaps i never made clear what i was asking.

I have multiple variables:

b1
b2
b3
b4
ect......

I need to loop though each variable in and obtain or set a value this is what i have so far, i just need to know how to reference the variable using the string?

Thanks,

Tommy
Dim co As Integer
Dim str As String
co = 1
Do Until co = i
str = "b" & co.ToString

' I now need to assign a value to a variable with the name specifyed in the string str

i = i+1
Loop

Open in new window

0
 
LVL 10

Accepted Solution

by:
joriszwaenepoel earned 500 total points
ID: 33595070
I don't know of any way to get a reference to a variable using its name as as string.

Can't you put them in an array of List?
Then you can reference them with their index:

b(0)
b(1)
b(2)

0
 

Author Comment

by:_Tommy_b
ID: 33595096
Thanks joriszwaenepoel,

Am new to programming and never thought of using an array...

This seams to be the soloution to my problem.

Again thank you very much..

Kind Regards,

Tommy
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33598678
*Just for the record, it is possible to do what your asking using REFLECTION...but we'd really need to know more about your application to decide which approach best fits your needs.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question