Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

ValueType reference in objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andre Azevedo - 22 Nov 2006 19:56 GMT
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
Peter Duniho - 22 Nov 2006 20:16 GMT
> 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?

No, it doesn't.  There might be a way to generate a reference to a local
variable, but if there is I don't know what it is, and for sure the code you
posted doesn't.  Except, of course, as the "by reference" parameter in the
constructor...but in that case, you still can't access the reference
directly, it just means that if you change the value of the parameter in the
constructor, the original variable passed in is changed as well.  That
behavior does not extend to other values to which the parameter is assigned.

Pete
parez - 22 Nov 2006 20:25 GMT
Hey

You could try creating a custom integer class and pass it to the your
cons instead of int
> > 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?
[quoted text clipped - 8 lines]
>
> Pete
David Boucherie & Co - 22 Nov 2006 20:16 GMT
Hey Andre...

>     public MyClass(ref int I)
>     {
[quoted text clipped - 3 lines]
> 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?

Value of I is copied to the location of i. Indeed, since MyClass is a
class, its memory is allocated on the heap. But because i is int, a
value type, the "=" operator copies its content, and does not assign
it's location (address).

Note: This is true for strings too, although they are reference types.
The "=" operator for strings is implemented to behave like a the
assignment of a value type.

Does this answer your question?

David
Andre Azevedo - 23 Nov 2006 00:14 GMT
Yes.
Thanks!

> Hey Andre...
>
[quoted text clipped - 18 lines]
>
> David
Jon Skeet [C# MVP] - 23 Nov 2006 19:23 GMT
> Note: This is true for strings too, although they are reference types.
> The "=" operator for strings is implemented to behave like a the
> assignment of a value type.

There's nothing special about how "=" is handled with strings. It's the
same as with every other reference type - it just copies the reference.

Strings just happen to be immutable, so things like Replace return a
reference to a new string, and if you do:

x += "fred";

that is the same as:

x = x + "fred";

where x + "fred" returns a new string which is the concatenation of x
and "fred".

Nothing special at all in there. Note that in fact you can't overload
the "=" operator in C# (fortunately, IMO).

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Shailen Sukul - 22 Nov 2006 23:01 GMT
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 :-)
Signature

Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)

> Hi all,
>
[quoted text clipped - 41 lines]
> --
> Andre Azevedo
Andre Azevedo - 23 Nov 2006 00:42 GMT
Hi,

I just ask because I really didn't know what happens with my code.
I have a thread that polls hardware events in a loop calling an unmanaged
function. The event structure has unions with value and reference types and
to receive the event structure I use IntPtr: (non-blittable)

For instance:

int eventSize = 0;
IntPtr event;

while(threadActive)
{

   event = Marshal.AllocHGlobal(256);

   int result = StaticClass.UnmanagedFunction(IntPtr event, ref eventSize);
   ....
   (parse IntPtr)
   ...

   Marshal.FreeHGlobal(event);

}

Now, I want to parse the event variable asynchronously. So I have to
encapsulate it in some class and this class is passe in
ThreadPool.QueueUserWorkItem method:

{
...
event = Marshal.AllocHGlobal(256);
int result = StaticClass.UnmanagedFunction(IntPtr event, ref eventSize);

MyContainerClass c = new MyContainerClass(ref IntPtr);  <----- What happes
here!
ThreadPool.QueueUserWorkItem(new WaitCallback(MyWaitCallBackMethod),
MyContainerClass);
...
}

void MyWaitCallBackMethod(object state)
{

   MyContainerClass c = (MyContainerClass) state;
   IntPtr event = c.event;
   ...
   (parse IntPtr)
   ...
   Marshal.FreeHGlobal(event);

}

That's why I ask about "ref" value parameters.
Thanks!

--
Andre Azevedo

> 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
[quoted text clipped - 77 lines]
>> --
>> Andre Azevedo
Jon Skeet [C# MVP] - 23 Nov 2006 19:25 GMT
<snip>

> That's why I ask about "ref" value parameters.

"ref" only makes a difference if you change the *parameter* during the
method. Other than that, it behaves just like any other parameter. In
particular:

myVariable = myParameter;

is just a copy of the value of myParameter to myVariable, whether the
myParameter parameter is passed by reference or not. There's no "link"
forged between myVariable and myParameter.

See http://www.pobox.com/~skeet/csharp/parameters.html for more on
parameter passing.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.