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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Content Value

Status
Not open for further replies.

CyberPat

Programmer
Joined
Jun 19, 2001
Messages
3
Location
CA
Hi all,

I have a string containing the name of a variable :
'---------------------------
Dim strVarName as string
Dim intNeedValue as integer

intNeedValue = 1234
strVarName = "intNeedValue"
'---------------------------
This is sort of a bad exemple but you get the point. I want to use the strVarName to get the value of intNeedValue. Is there an existing function for that ? I cant find it or o dont know how to do it.

Thx
 
You can't do that in VB.

But, what you could try is using a collection(or dictionary) and then use the name of the key, instead of the variable name. This would work better if you had a lot of var's that you needed to use this way.

i.e

dim cMyVars as New Collection
dim sVarName as string

cMyVars.Add "iNeedValue", 1234
sVarName = "iNeedValue"

debug.print cMyVars.Item(sVarName)


HTH


______________________________
- David Lanouette
- Lanouette Consulting, LLC
- DLanouette@Computer.org
 

It's possible that an Eval method would help a little. Vb doesn't have one, but the Scriptcontrol obbject does. Add a reference to MS Script Control 1.0, then have a look at the following code (not a well-constructed solution to your problem, more a pointer of a possible avenue of further investigation):

Dim wscript As ScriptControl

Set wscript = New ScriptControl
wscript.Language = "vbscript"

Dim strVarName As String
Dim intNeedValue As Integer

wscript.ExecuteStatement ("intNeedValue = 1234")
strVarName = "intNeedValue"

wscript.ExecuteStatement ("result = " + strVarName)
MsgBox wscript.Eval("result")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top