Can a reference be 'boxed' and 'unboxed' ala 'pointer to pointer'

G

Guest

HI

can i box and unbox a reference so that i can achieve a 'pointer to pointer' effect. i want to pass a reference as a parameter to a function and then have the function change what the reference is pointing to..

many thank
tham
 
P

Philip Rieck

You don't need any boxing if you have a reference already. You just need to
make the paramater a "ref" param.

....
// the called function:
public void switchReference( ref Hashtable collection)
{
collection = new Hashtable();
}
....
//calling it
....
Hashtable a,b;
a = new Hastable();
b = a;
//now, both a and b referr to the same Hashtable
switchReference(ref a);
//now, b still refers to the original Hashtable, a is a separate one.




tham said:
HI,

can i box and unbox a reference so that i can achieve a 'pointer to
pointer' effect. i want to pass a reference as a parameter to a function and
then have the function change what the reference is pointing to...
 
G

Guest

Hi,
HI,

can i box and unbox a reference so that i can achieve a
'pointer to pointer' effect. i want to pass a reference as
a parameter to a function and then have the function change
what the reference is pointing to...

many thanks
tham

There's no direct representation of "pointer to pointer"
in C#, like Philip & Jon said before.

But there is indirect version object[], that can box
any type e.g. :

object[] myBox=new object[1];
myBox[0]=new MyClass();
myBox[0]=2.3;

This table ("myBox") can be parameter for any fuction, and
it should work like "pointer to pointer".

Regards

Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top