[.net] C# safe pointers

Started by
6 comments, last by ELFanatic 16 years, 1 month ago
I have an issue that has occured while programming in C# that I didn't think would occur. I pass an object by ref and try to assign it to a private member variable but when I change the value of the private member I realized that the private variable was not pointing to the same data that was passed in. Is there anyway I can do this with a safe pointer or do all safe pointers get assigned to clones. It wouldn't be too big of an issue to do it unsafely but I'd prefer to do it safely.
Advertisement
Could you post some code to show us what you're doing. The class with the method you're implementing would be the best. It sounds like the object is being cloned when you don't want it to be, but it's hard to tell without the code.

Also, using the ref keyword is unnecessary when dealing with objects as objects are passed by reference by default.
Make a class to wrap the value-type, and replace all uses of the value-type with that class. That's the only safe way to share references to value types.
You really don't want to do it unsafe unless you really have to. For this it should not be needed.

Can you please post the code so we can see what is going on?

theTroll
Quote:Original post by ELFanatic
I have an issue that has occured while programming in C# that I didn't think would occur. I pass an object by ref and try to assign it to a private member variable but when I change the value of the private member I realized that the private variable was not pointing to the same data that was passed in. Is there anyway I can do this with a safe pointer or do all safe pointers get assigned to clones.

It wouldn't be too big of an issue to do it unsafely but I'd prefer to do it safely.


It sounds like you are doing this:

void Foo(ref object myObject);

What that does is pass in the reference of the reference you have. So essentially the data you are getting is a reference to the reference of the object. What you really want is just this:

void Foo(object myObject);

Because all classes in C# are passed by reference by default (hence their name of 'reference type').

If that isn't the case, please just ignore me and post your code for us to all look at.
sorry I caused a lot of issues with the passing by reference comment. I just did a public void foo(aClass theObject)

I was having issues with assigning an object of type Matrix from the XNA library (I forget the namespace now) I was just stupid, I'm sure it does a deep copy and passes back the copy but for some reason I was expecting it to act more like a pointer or reference assignment.

The way I figure it works is like this.
Matrix r;
Matrix t = r; //copy constructor called and passed back.

But I was thinking it was going to work like this.
Matrix r;
Matrix * t = &r

Basically, I wasn't thinking.

So, correct me if I'm wrong, there's no safe way of doing the latter, is there? If I ever assign an object it'll always get a copy, there's no way of just getting it to reference the object. (though the wrapper class is pretty ingenius if all else fails) Just for future knowledge. I fixed this issue a couple days ago. Sorry for the late reply.
C# allows you to define two different kinds of types: value types and reference types. Objects you create using the class keyword are reference types, whereas object created using the struct keyword are value types.

The distinction is important, as it affects several properties about your object. One of them is the way they are passed between functions and and operators, as you yourself have found out. Value types (which includes the Matrix type) are always copied. Reference types only have their reference (acts like a pointer to the type) passed around.

While there aren't any explicit provisions in the language for storing references to value types, if you need the value type to be changeable from within a method, you can pass it using the ref keyword, which will pass a reference to the type.

I suggest reading up on value types and reference types to get a better handle on the subject.
Mike Popoloski | Journal | SlimDX
Quote:Original post by Mike.Popoloski
C# allows you to define two different kinds of types: value types and reference types. Objects you create using the class keyword are reference types, whereas object created using the struct keyword are value types.

The distinction is important, as it affects several properties about your object. One of them is the way they are passed between functions and and operators, as you yourself have found out. Value types (which includes the Matrix type) are always copied. Reference types only have their reference (acts like a pointer to the type) passed around.

While there aren't any explicit provisions in the language for storing references to value types, if you need the value type to be changeable from within a method, you can pass it using the ref keyword, which will pass a reference to the type.

I suggest reading up on value types and reference types to get a better handle on the subject.


aww I did not realize it was a struct. That does explain a lot. Perhaps your right and I should read up more on structs. I forget that their different from C. I think you pretty much answered all my questions. Thank you.

This topic is closed to new replies.

Advertisement