Using content of variable

  • Thread starter Thread starter tino
  • Start date Start date
T

tino

Years ago there used to be a feature in dBase and FoxPro
where the content of a variable could be used a variable
name:

Var1 = "Var2"
Var2 = blank
&Var1 = "Hallo"

this way Var2 would get the value "Hallo" assigned.
By prefixing Var1 with the empersand the program would use
the content of Var1 rather than Var1 itself.

Can this be done in VB.NET?

thanks
tino
 
Hi Tino,

Strange patterns occur sometimes. The same question but from a different
viewpoint appears only minutes apart from yours. So the same answer for both!
;-)

Have a look and see if CallByName is of any use to you.

If that's not suitable, the next to look at is the Type class. (use
GetType on the object which owns the method/member that you are interested.
This has GetField/GetFields and GetMember/s which may do the job.

Regards,
Fergus
 
* "tino said:
Years ago there used to be a feature in dBase and FoxPro
where the content of a variable could be used a variable
name:

Var1 = "Var2"
Var2 = blank
&Var1 = "Hallo"

this way Var2 would get the value "Hallo" assigned.
By prefixing Var1 with the empersand the program would use
the content of Var1 rather than Var1 itself.

Can this be done in VB.NET?

There is no way to do that with local variables. For other public
fields, properties or methods have a look at the thread above this one.
 
Hi Tino,

If you have any question on this issue please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Tino,

Based on my understanding, what you want to do is to access an
variable(Var2) by using another variable(Var1)'s value., which is a name
string.

I think this feature is usually supported by scripting programming
language, i.e. the language will execute code line one by one.
Implementing such a feature is resource-consuming and can cause performance
hit, so compiled languages like C/C++,VB, VB.NET and etc. never supported
the feature.
In such languages, compiler usually does not maintain variable names in the
executables so identifying a variable though a string name is not feasible.

Some controls (not the language) themselves maintain a name so they can be
identified using a name string, like for Controls in VB.

In .NET if you just want to identify a field (not local variables) you can
achieve that by using the reflection as Ken said. You may refer the article
in MSDN.
Dynamically Loading and Using Types
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpcondynamicallyloadingusingtypes.asp

If you simply want to bind two variables together so changing one will
cause the other to change as well you can use a reference type (class) to
encapsulate the value.

Can you tell me what do you want to do in the project?
I think there may be a better solution for you.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top