Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The content of a variable with a name specified as a string 1

Status
Not open for further replies.

andreigogan

Programmer
Jan 24, 2002
3
0
0
RO
I want to find the content of a variable with a name specified by a string.

For example, let's say that i have an variable
var="var2" and another variable
var2="var3"
I want to find the content of the variable that have the name specified in the content of the variable var
 
I think most programmers wish you could do something like that. The closest function similar to it in VB is the CallByName function, however that will not do exactly what you want. You could create a collection of variables making the key the name of a variable. Like This:


Dim col As New Collection
Dim Var1, Var2, Var
Dim Result As String

col.Add Var1, "Var1"
col.Add Var2, "Var2"

Var1 = "contents of var1"
Var2 = "contents of var2"

Var = "Var1"
Result = col(Var) 'returns contents of Var1
Var = "Var2"
Result = col(Var) 'returns contents of Var2


The catch is that the collection seems to be a copy of the variable and not a pointer (by reference) to the variable. So when the variable changes again it does not change in the collection.

You can also try this:

'On a form called form1
Public Var1 as string
Public Var2 As String

Sub Form_Load()
Var1 = "Test contents of Var1"
Var2 = "Test contents of Var2"
Msgbox Var("Var1")
Msgbox Var("Var2")
End Sub

Public Property Get Var(VarName as string) as string
Var=CallByName(me,VarName,VbGet)
End Property


Changes made to the variable will be reflected at all times.
If there is a better way, I would like to know also.

 
Not strictly of much help, but Visual FoxPro does all this...

Macro Substitution is the first point of interest... If you have a string containing a portion of source code, you can just use @StringName - and watch it fly...

The Evaluate function would suit this guy's question perfectly... You just pass it a string, and it just passes you a result. Lovely!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top