tags:

views:

31

answers:

2

Hi, I believe this is a tough one...

I'm trying to create a collection of variable binding in a class.

It is intended to look something like this:

Dim x as integer, s as string
Dim c as new VBindClass

x = 1
s = "Hello"

c.Add x, s
Debug.Print c.value(x) '= 1
Debug.Print c.value(s) '= "Hello"

Is there some function that allow us to retrieve a unique ID for a given variable and also get/set based on variable?

Update:

At last, I've managed to found the answer. Here's the code:

Dim gh As Runtime.InteropServices.GCHandle = Runtime.InteropServices.GCHandle.Alloc(obj)
Return Runtime.InteropServices.GCHandle.ToIntPtr(gh).ToInt64
A: 

IF I understand correctly what you are looking for you might want to have a look at using

Dictionary Class

astander
I considered this but `Debug.Print c.value(s) '= "Hello"` threw me off.
ChaosPandion
+1  A: 

You cannot bind directly to method variables, since a: there is no reflection to them, and b: they may not actually exist by the time the compiler is through. Also, in implementation terms it would be very confusing for something like a class (VBindClass) to have access to the current stack position (since the two would be miles apart).

You can reflect on fields though (variables on a class / struct) using reflection (Type.GetFields(...), usually needing the non-public | instance binding-flags).

For your example, "captured variables" might be of use, but you'd need to be using lambdas / expressions for that to work. But in C# terms:

int x = 1;
Action a = () => Debug.WriteLine(x);

// can now hand "a" anywhere, including to other classes, into events etc

x = 27;
//now invoke a **from anywhere** - it'll show the current value (27)
a();
Marc Gravell
Thanks. However, I'm looking for some function that returns a unique ID (pointer would be best) but I've read many places that .Net is not pointer friendly. I only use the pointer as a unique identifier for a variable, in order for the class able to get/set the values.FYI, I've got a class in VB6 that does this and looking at migrating it to VB.Net.
Davis