M
Mark Oliver
Hi,
I want to pass a reference to a char type to a class constructor, then
read/write to the passed char later in my code. Are the below Asserts
right?
Thanks,
Mark
public class pinTest
{
private IntPtr ip;
public pinTest(ref char ch)
{
unsafe
{
fixed(void *vp=&ch)
ip=new IntPtr(vp); // is ip valid after fixed statement if
'ch' was value type?
}
}
public char c
{
set
{
unsafe
{
*((char *)(ip.ToPointer()))=value;
}
}
get
{
unsafe
{
return *((char *)(ip.ToPointer()));
}
}
} // public char c
} // class pinTest
public void Main()
{
char a='a';
pinTest pa=new pinTest(ref a);
char [] cb={'b'};
pinTest pb=new pinTest(ref cb[0]);
/*
..
..
..
*/
Trace.Assert(a==pa.c,"Pass because 'a' was value type?");
Trace.Assert(cb[0]==pb.c,"maybe Fail because 'cb' was reference type?");
}
I want to pass a reference to a char type to a class constructor, then
read/write to the passed char later in my code. Are the below Asserts
right?
Thanks,
Mark
public class pinTest
{
private IntPtr ip;
public pinTest(ref char ch)
{
unsafe
{
fixed(void *vp=&ch)
ip=new IntPtr(vp); // is ip valid after fixed statement if
'ch' was value type?
}
}
public char c
{
set
{
unsafe
{
*((char *)(ip.ToPointer()))=value;
}
}
get
{
unsafe
{
return *((char *)(ip.ToPointer()));
}
}
} // public char c
} // class pinTest
public void Main()
{
char a='a';
pinTest pa=new pinTest(ref a);
char [] cb={'b'};
pinTest pb=new pinTest(ref cb[0]);
/*
..
..
..
*/
Trace.Assert(a==pa.c,"Pass because 'a' was value type?");
Trace.Assert(cb[0]==pb.c,"maybe Fail because 'cb' was reference type?");
}