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

Variable substitution

Status
Not open for further replies.

PizMac

Programmer
Nov 28, 2001
90
GB
I want to declare an array of variable names and contents and then code a loop which sets them
e.g. set var1 to "a", var2 to "x" using an array like
dim a(2,2) as string
a(1,1)="var1"
a(1,2)="a"
a(2,1)="var2"
a(2,2)="x"

'and then
for i=1 to 2
&a(i,1)=a(i,2)
loop

but...&a isn't right - what do I need instead please?
I've looked at the archives but can't see what I want..
Thanks
 
parameterise the settings
e.g. set var1 to "a" and var2 to "x" without having to change the program - the array will be set up from an external file so I can add an extra var3="y" or change var1="?" without any program intervention
 
Hmm...

Are you familiar with .ini files? If so, you might want to look at the GetPrivateProfileString API function.

Another option would be to use a dictionary object.

What I'm saying is that you're trying to write your own code to something that is catered for quite well by features that are already available to you.


mmilan

 
NO- I know what I WANT to do - I can do it in other languages (e.g. &name in FoxPro) - I merely want to store a variable name in another variable and then set the first variable to something
e.g. VarStore="Var1"
now set the variable Var1 to "xyz" by referencing VarStore

as another e.g. - I may want to perform the same procedure for various variables but only code it once
e.g.
VarStore="Var1"
CalcCode
VarStore="Var2"
CalcCode

procedure CalcCode
do something with the variable whose name is stored in VarStore

 
Ok - Macro substitution ala Foxpro.

The short answer is that VB does not support it.

This is actually a good thing. Suppose some halfwit user finds your file and "plays" with it... The values you could trap in your code, but changing the actual variables that get assigned - BIG NO NO...

Use GetPrivateProfileString. It may mean a little more coding, but it could also mean a lot less support.

mmilan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top