RE: ValueType reference in objects
- From: Shailen Sukul <shane@xxxxxxxxxxxxx>
- Date: Wed, 22 Nov 2006 15:01:02 -0800
If your goal is to simply find a way to pass in integer pointers, then the
following code will work. I would like to point out that using pointers and
unsafe code is NOT recommended and should be reserved for special case
scenarioes.
You will also need to check the "Allow unsafe code" option in the Build tab
of your project properties.
unsafe class MyClass
{
public int* i;
public MyClass(int* I)
{
i = I;
}
}
class Program
{
unsafe static void Main(string[] args)
{
int i = 50;
MyClass c = new MyClass(&i);
Console.WriteLine(*c.i);
i = 10;
Console.WriteLine(*c.i);
Console.ReadLine();
}
}
HTH :-)
--
Good luck!
Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)
"Andre Azevedo" wrote:
Hi all,.
With the followin code:
class MyClass
{
public int i;
public MyClass(ref int I)
{
i = I;
}
}
class Program
{
static void Main(string[] args)
{
int i = 50;
MyClass c = new MyClass(ref i);
Console.WriteLine(c.i);
i = 10;
Console.WriteLine(c.i);
Console.ReadLine();
}
}
I´ve got:
50
50
The i field from c object didn´t change its value.
The i field doesn´t point to i address.
C# copies the value from the i variable to the object heap field i.
Is this correct? Heap object doesn´t point to stack variables?
Please help.
TIA,
--
Andre Azevedo
- Follow-Ups:
- Re: ValueType reference in objects
- From: Andre Azevedo
- Re: ValueType reference in objects
- Prev by Date: An instance of a form in another
- Next by Date: Re: try...catch and local variables
- Previous by thread: Re: ValueType reference in objects
- Next by thread: Re: ValueType reference in objects
- Index(es):
Relevant Pages
|