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

Building Variable Names from literals 1

Status
Not open for further replies.

mushin

Programmer
Oct 4, 2000
49
0
0
dim tmp1, tmp2, tmp3, myVar as string.
dim myNbr as integer

myVar = "some data values"
myNbr = 1

"tmp" & myNbr = myVar

I need to build my variable name and assign its value based on values in other variables as above.

in the example above I want a variable called tmp1 to be assigned the contents of myVar.

Some suggested usung a function called Eval() but I can find no reference to it in VB....

Any ideas ????

 
Eval() is a VBA function
What do you want to do with the variable tmp1? If you wish to assign a value to a contril with that name then rather use the following:
Me("tmp1") = Value.

Otherwise send more detail
 
mushin -

It sounds like you want to concatenate a variable name together, and then use it as it's own variable at runtime.

Sorry, but this can't be done (at least not very easily -- it would require a C DLL at a minimum). VB has to know about ordinary variables at compile-time in order to allocate space for them.

It sounds like you need to use an dynamic-sized array. Do this:

Dim A() as Long

' This creates the first element and stores a value in it
Redim A(1)
A(1) = 1234

' This wipes out the previous contents and creates room
' for three elements
Redim A(3)

' This saves the previous contents, and creates room for
' two more elements
Redim Preserve A(5)

' This saves the first two elements, and drops the
' last three
Redim Preserve A(2)


Your other option, if you're trying to store object variables or variant variables is to use a collection

Dim Col as New Collection
Dim MyItem as MyObject

Set MyItem = New MyObject
With MyItem
[tab]'Set properties, etc.
End With

Col.Add MyItem

Collections have the advantage of being able to insert new items before/after other items. Disadvantage (at least until VB7 comes out) is that they can't hold strings, longs, etc. unless you put them in a variant first.

Chip H.
 
Actually, you can do this in VB. There is a bit of a cludge that will simulate this. You could use a property bag to store all of your data. The data contained within the property bag can be accessed using pure string names (including concatenated strings). See the example below . . .



Dim objPropertyBag As PropertyBag
Dim strMyVariableName As String
Dim vntMyData As Variant

Set objPropertyBag = New PropertyBag

vntMyData = 10
strMyVariableName = "SomeNumber" & "ThatIWantToKeep"
objPropertyBag.WriteProperty strMyVariableName, vntMyData
vntMyData = objPropertyBag.ReadProperty(strMyVariableName)




Granted, this is not the most elegant method to use, but it would work (assuming I understand what you are trying to do). Your other option would be to use a collection and use the string index values of each item in the collection to access you data. See the next option . . .


Dim colData As Collection
Dim strMyVariableName As String
Dim vntMyData As Variant

Set colData = New Collection

vntMyData = 10
strMyVariableName = "SomeNumber" & "ThatIWantToKeep"
colData.Add Item:=vntMyData, Key:=strMyVariableName
vntMyData = colData.Item(strMyVariableName)



Once again, this is a work around, but it will work. As ChipH said, there is no direct way to get at standalone variable as you had previously mentioned. You will need to store you data in some object that understands how to use strings as indexes to retrieve data.
- Jeff Marler
(please note, that the page is under construction)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top