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

Using a Alias for a Variable?

Status
Not open for further replies.

CyberNight

Programmer
Mar 7, 2004
1
US
OK heres on odd question,
if i have two variables
Dim Test as string
Dim HAPPHAPPY as string

Set one of them with misc data and the other with the name of the first variable:
HAPPYHAPPY = "JoyJoy"
Test = "HAPPYHAPY"

Is there a way to use the variable Test to access the contents of the HAPPHAPPY variable. In essence using one variable as a alias for the other one. Im not creating a new variable im trying to access it through another varaibles contents. This is just an example i acctually am looking for the actual effect.

Thanks

CyberNight
 
If you mean Macro Substitution where something like

test2 = &Test

would assign "JoyJoy" to test2 then no.

There are lots of other ways to accomplish the same effect though. Arrays of types with the type having a name and a value, collections etc.
 
Take a look at:

Honestly, in VB6, the language isn't really set up to do this. I wouldn't use it.

Be aware that this keyword has been removed from the language in VB.NET (since strings are objects in .NET, there's no need for it).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
You can do it through a VBScript proxy as well, but it isn't exactly a clean method.
 
Oddlt, however, this is standard behaviour in parameter passing.

So:

Dim S as string

S= "Happy"

call Modify(S)
debug.print S

...

private sub Modify(X as string)
X= "Joy Joy"
end sub



X is an alias for S in this example.


 
Well now you are talking about pointers, that's a different issue and I don't see anything wrong with using them. But its not going to do what the first question asked about.

You still need to reference the actual variable to get the pointer, not a string containing the variables name.
 
CyberNight's question could have been read either way -- using pointers, or altering another variable by a name stored in a variable.

The first is possible under VB, but not recommended.
The 2nd may be possible in the VBscripting engine, but I really wouldn't do it.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
If the variables you are using are declared as public variables of a class module (like your main form) then you can use the CallByName function to access and modify the value of a variable knowing its name (stored in a string).
___
[tt]
Option Explicit
Public HAPPYHAPPY As String 'must be public here.

Private Sub Form_Load()
'initialize the HAPPYHAPPY variable.
HAPPYHAPPY = "JoyJoy"

'store the name of this
'variable in another variable.
Dim Test As String
Test = "HAPPYHAPPY"

'get the value of the variable
'whose name is stored in 'Test'.
MsgBox ValueOf(Test)

'assign a new value to the variable
'whose name is stored in 'Test'.
ValueOf(Test) = "NewValue"

'verify the new value of the
'variable HAPPYHAPPY directly.
MsgBox HAPPYHAPPY
'
End
End Sub

Public Property Get ValueOf(Name As String)
ValueOf = CallByName(Me, Name, VbGet)
End Property

Public Property Let ValueOf(Name As String, ByVal Value)
Call CallByName(Me, Name, VbLet, Value)
End Property[/tt]
___

Note that this method of accessing/modifying variable will only work as long as you are dealing with public variables in a class module. Variables in standard modules, or local/private variables cannot be accessed this way.
 
All righty then, my last word on this one...

I'm pretty sure what he was asking was is there Macro Substitution in VB like there is in interpreted languages like FoxPro (so that you can change the actual text of an instruction at run time).

Answer: No.


 
Wel, that's one interpretation

The way I interpreted the question was whether anything like macro substitution was possible. To which the answer has to be (as you yourself said) yes, with the caveat that none of the solutions are as clean as a built-in macro substitution (which, as you say, VB doesn't have)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top