Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointers in C#

Started by FenixRoA Nov 2, 2004 at 6:37 PM 3 replies 1.3k views
Original Post
FenixRoA
FenixRoA
I am making a program that needs to keep tracking of a particular event variable. So I'd like to pass the event variable as a reference or as a pointer variable up to another object. How would I change the event variable to a pointer variable and back again? (I'm using C# VS 2002 but I have access to VS 2003 if that makes any difference). Thanks for your time and consideration, FenixRoA
darookie
darookie
Are you sure the variable isn't passed by reference already?
FenixRoA
FenixRoA
This might be the case but when I say the following

RandomObject Whatever;
Whatever = PassedInObject;

Where PassedInObject is of type "RandomObject" would it not make a copy of said object? or would it only store a new reference to the same copy? If I pass in a variable using the "ref" keyword and do the same code as above would it create a copy in Whatever from the object referenced by PassedInObject or would it just store a reference to whatever object I passed in, in Whatever?

Please help me,

Thanks for your time and consideration,
FenixRoA

I realize these questions a probably menial and below your consideration but please, just spare me 2 seconds and hit the reply button.
Washu
Washu
In C# everything is passed by value, unless otherwise specified. However, objects are stored as references, so you are passing the value of the reference. What this means is that f(Obj) will pass the value of the reference Obj. This will allow you to change the object Obj points to. If you use the ref (or out keyword) keyword, then you are passing a reference to the reference. This allows you to change where Obj itself points to, aka: f(ref Obj) could be some form of a factory function that would create an object and assign it to Obj.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
jonnii
jonnii
i think it's easier to think of classes being passed by reference and structs being passed by value.
-jonnii=========jon@voodooextreme.comwww.voodooextreme.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.