469,270 Members | 1,108 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

C# Object Reference?

I know this is possible in C++ with pointers but dont know how to do it in C#.

Simplified Sample Code and Comments

//create object A
object A = new object();

//create object B
object B = A;

//create object C
object C = new object();

//I need to change Object A to point to the contents of Object C only using Object B
May 24 '07 #1
10 33508
Plater
7,872 Expert 4TB
Why do you need two things pointing to the same object? I've not been able to come up with a use for it.
May 24 '07 #2
mwalts
38
I know this is possible in C++ with pointers but dont know how to do it in C#.

Simplified Sample Code and Comments

//create object A
object A = new object();

//create object B
object B = A;

//create object C
object C = new object();

//I need to change Object A to point to the contents of Object C only using Object B
Hmm, don't know how to do this directly, however if you create a function and pass it by reference it will exhibit that behaviour.

i.e

//in main or whatever
//create object A
object A = new object();
someFunction(A);
.
.
.
private void someFunction(ref object B)
{
object C = new object();
B = C;
}


Now A has been changed to C through B

Not sure if that helps you, I have to admit I haven't come up with a good reason to do it directly in C# yet, though I seem to remember using it more then once in my old C days

-mwalts
May 24 '07 #3
Plater
7,872 Expert 4TB
Hmm, don't know how to do this directly, however if you create a function and pass it by reference it will exhibit that behaviour.

i.e

//in main or whatever
//create object A
object A = new object();
someFunction(A);
.
.
.
private void someFunction(ref object B)
{
object C = new object();
B = C;
}


Now A has been changed to C through B

Not sure if that helps you, I have to admit I haven't come up with a good reason to do it directly in C# yet, though I seem to remember using it more then once in my old C days

-mwalts
Well, now A has been set to the contents of C, but changing A would not also change C. They don't occupy the same memory, they are still just copies of one another.

If you want to do memory manipulation you have to make the jump into unmanaged code (unsafe code, check msdn on it) and that gets real nasty real fast.
May 24 '07 #4
The real situation is I have a field from a datarow that I put into a collection.
Then latter I have the collection but not the datarow(but the datarow still exists). I want to set the datarow field to a new value but feel it will only change the collection to the new data and not the orginal datarow field.
May 24 '07 #5
It would be easy if only C# supported pointers to Reference Types
May 24 '07 #6
mwalts
38
It would be easy if only C# supported pointers to Reference Types
Yeah, but direct memory manipulation would kinda defeat the whole managed sandbox concept period.

For normal reference types you would be able to change their values from the fields located in your collection... for boxed value types and immutable reference types (which are of course the most common in DataRow's) I'm not sure what the behavior would be. Probably won't work since you would have to unbox it to change the value... But it might be worth a try to just change the collections value and see what you get

You might have to put the DataRow into the collection instead of the individual field (or some such work around)

Good luck,

-mwalts
May 24 '07 #7
The real situation is I have a field from a datarow that I put into a collection.
Then latter I have the collection but not the datarow(but the datarow still exists). I want to set the datarow field to a new value but feel it will only change the collection to the new data and not the orginal datarow field.
Why don't you want to store a reference to the object itself?

Also, you could implement an event-type system to update the datarow when the value in your collection changes.
May 25 '07 #8
Yeah, I had to put the datarow into the collection

I suppose for more generic answer
if you encapsulate a object into another object you can pass the outer object and then change the inner object without loosing the reference?

or something like that
Jun 5 '07 #9
class WrapperObject
{ public TrackedObject obj }

//now you can do this.
WrapperObject A = new WrapperObject();
A.obj = new TrackedObject();
WrapperObject B = new WrapperObject();

TrackedObject C = new TrackedObject();
B.obj = C;

//now A.obj = C
Jun 23 '07 #10
Plater
7,872 Expert 4TB
Anything created with the "new" keyword will be passed by "reference" automatically.
So if your function takes in as an argument an object,
any actions done on that object withen the function are actually done to the object that was passed in.

I used to do this when I needed a cheap way to get one form to talk to another, I would pass a textbox into the contrustor. (Note: don't actually pass textboxes and stuff like that)
Jun 26 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Pkpatel | last post: by
7 posts views Thread by Brett | last post: by
6 posts views Thread by kalaivanan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.