There is a simple way to refer to the variable name rather than then variable value, but I can't remember or locate how to do it. I forget the term, somthing like convert the name to a string?
such as:
apple=20
lemon=33
banana=45
send one of these names (call it "xfername")
xfername=xfername +4
msgbox (" increased to " & xfername)
Note that what I desire is that if apple were the "sent one", it would have an updated final value of 24. If banana were sent, it would have a value of 49.
Re: passing variable name rather than the variable
I really need to have a variable or some function hold the name of the variable (apple, banana, etc) so that I can do some processing on it much later in the program. There is some decision logic early on to determine which variable is being updated later on.
Re: passing variable name rather than the variable
Create a TYPE of your own, for FRUIT
Code:
Type SystemInfo
CPU As Variant
Memory As Long
DiskDrives(25) As String ' Fixed-size array. if you need one for each
VideoColors As Integer
Cost As Currency
PurchaseDate As Variant
End Type
or create an array of them, and assign values
Code:
Dim AllSystems(1) As SystemInfo
AllSystems(0).CPU = "386SX"
AllSystems(0).DiskDrives(2) = "100M SCSI"
You can pass procedure arguments using a user-defined type.
Code:
Sub FillSystem (SomeSystem As SystemInfo)
SomeSystem.CPU = lstCPU.Text
SomeSystem.Memory = txtMemory.Text
SomeSystem.Cost = txtCost.Text
SomeSystem.PurchaseDate = Now
End Sub
Re: passing variable name rather than the variable
I think I understand what vbcandies wants. In fact everything was easy if we had pointers in VB. So we could store a pointer to a variable somewhere and know later which variable that was. However, you cannot pass the name of a variable to a function or sub, or store the name of a variable in a string.
Unfortunately we haven't pointers and we can't really simulate them.
What you can do is: you can implement an array (of fruit), wher fruit(0) is apples, fruit(1) is lemons, fruit(2) is bananas. Then you can store the index of the array element or pass it to a sub to make changes. If you assign constants you keep the overview in your program.
The actual value is stored in an array, but you use the index to access it.
Code:
Const FRT_apple = 0
Const FRT_lemon = 1
Const FRT_banana = 2
Private Fruit(2) As Integer
Private LastFruit as Integer
Private Sub ChangeFruit(FruitIndex as Integer, Increment as Integer)
Fruit(FruitIndex) = Fruit(FruitIndex) + Increment
End Sub
'now you can do:
ChangeFruit(FRT_apple, 10)
LastFruit = FRT_apple
...later then
ChangeFruit(LastFruit, -10)
Another idea is, to create a simple class Fruit which contains one property 'Count'.
The variables will be objects references which can be seen like pointers.
You'd need a class module named 'Fruit'
In the simplest way it contains only one line
Code:
Public Count As Integer 'or Long if you like
In your main program you have to declare and use your objects like that:
Code:
Private apple As New Fruit
Private lemon as New Fruit
Private banana as New Fruit
Private LastFruit as Fruit
Private Sub ChangeFruit(FruitVar as Fruit, Increment as Integer)
FruitVar.Count = FruitVar.count + Increment
End Sub
ChangeFruit(apple, -10)
Set LastFruit = apple
... later then
ChangeFruit(LastFruit, 20)
Re: passing variable name rather than the variable
If the names are known at design-time, you could use a Select Case statement:
Code:
Function GetVar(Name As String) As Long
Select Case Name
Case "Apple"
GetVar = Apple
Case "Lemon"
GetVar = Lemon
Case "Banana"
GetVar = Banana
End Select
End Function
So you could use it like:
Code:
Result = GetVar("Banana") + 4
However, you can get a pointer to VB variables using VarPtr(), or StrPtr() for strings. Though this doesn't sound quite like what you're looking for.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
Re: passing variable name rather than the variable
I also considered VarPtr() in the first place, but then I found no means to pass it as a reference to a function or sub.
You could store a VarPtr() in a Long but I didn't find a means to pass it to a sub, so as the sub would accept it as a reference to a variable.
You would have to declare the parameter ByRef, but pass the VarPtr() variable ByVal
Code:
Dim apple as integer
Sub ChangeFruit(ByRef FruitVar as Integer, Increment as Integer)
FruitVar = FruitVar + Increment
End Sub
Dim LastFruit as Long
LastFruit = VarPtr(apple)
ChangeFruit ByVal LastFruit, -10
Re: passing variable name rather than the variable
Wiz bang--thanks, but your suggestion is sort of the reverse of what I want===I need to later on UPDATE the variable value (being banana, apple, etc) based on previously slecting which of the variables I want to update.
I suppose DGLIENNA/WOF's idea is the only way to go..."problem" is I've already using a bunch of different variable names here & there in the code, so now I have to gather them up into a multidimensional array or TYPE!!
Last edited by vbcandies; January 7th, 2010 at 12:51 AM.
Re: passing variable name rather than the variable
After considering all the possibilities I like the Object solution best.
It allows closest what you want: store a pointer to a variable for later use, no matter which one it was.
Arrays seem apropriate too, but all the changes seem more complicated than to write one simple one-line-class as I described.
Change the variable declarations, and where the variables are used, you add the Value property (or Count).
Code:
Dim apple as New Fruit
apple.Value = 10
Dim FruitPointer as Fruit
Set FruitPointer = apple
Re: passing variable name rather than the variable
Originally Posted by WoF
I also considered VarPtr() in the first place, but then I found no means to pass it as a reference to a function or sub.
You could store a VarPtr() in a Long but I didn't find a means to pass it to a sub, so as the sub would accept it as a reference to a variable.
You would have to declare the parameter ByRef, but pass the VarPtr() variable ByVal
Code:
Dim apple as integer
Sub ChangeFruit(ByRef FruitVar as Integer, Increment as Integer)
FruitVar = FruitVar + Increment
End Sub
Dim LastFruit as Long
LastFruit = VarPtr(apple)
ChangeFruit ByVal LastFruit, -10
Would be nice, but does not work.
Sub
Actually, CopyMemory can do the trick.
Originally Posted by vbcandies
Wiz bang--thanks, but your suggestion is sort of the reverse of what I want===I need to later on UPDATE the variable value (being banana, apple, etc) based on previously slecting which of the variables I want to update.
I suppose DGLIENNA/WOF's idea is the only way to go..."problem" is I've already using a bunch of different variable names here & there in the code, so now I have to gather them up into a multidimensional array or TYPE!!
Well, I'm partial to UDT arrays. Incredibly useful, and they also make it sooo easy to store and retrieve data from a file. If I understand what it is you want to do, attached is an example of three ways to do it.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
Re: passing variable name rather than the variable
WizBang:
WOW---that is quite an impressive demo...thanks for the great ideas...there seems to be so many obscure and hidden functions to work with...not sure how you run across all this...I guess. you call it: Experience..
I alway get great thoughts from everyone on this forum.
Re: passing variable name rather than the variable
Yes, good one, Wizbang. CopyMemory is always a way to workaround things VB6 does not have. But it requires you to know exactly what you are doing, because any wrong memory address would crash the program badly.
Re: passing variable name rather than the variable
Yeah. That's why I'd rather use the object oriented approach. It is perfectly safe.
What else are Class modules good for if we can achieve everything with CopyMemory.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.