[.net] Storing reference of a value type

Started by
6 comments, last by RipTorn 16 years, 3 months ago
Is there a way, aside from using unsafe code, of storing a reference to a value type? For example, something like passing a ref float to a method and then storing that reference?
Advertisement
You mean, for example, having a reference to a float?

If that becomes necessary, I would store the value in some kind of class, and reference that class. That way, I can control exactly how the value is accessed. Anything that must access an instance of one of these objects then has a pointer to it.

(take, for example, my characters. They each have a position and direction vector, which the AI uses.)
I just wanted to see if he would actually do it. Also, this test will rule out any problems with system services.
Yeah, that's pretty much what I want to do. I thought of making a specific class with the variable to do that, but ideally it would be generic enough that you could reference a float or Vector from anywhere to do it.
Unless there is a specific relationship between the class being referenced, and the referencer(for want of a better generic word) it can get messy very quickly. e.g, you delete the class containing the reference, and the referencer is now reading empty pieces of memory. It looks like its working for a while, then it all goes boobies up.

Or, you have 10 objects referencing the same class. the first moves the character forwards. The second moves him back. The third spins him around. The fourth does something else, etc. By the time you render him, he is buried head first, up to the waist, in the ground with his pants down because a physics constraint failed.

This has never happened to me (whistle whistle whistle)
I just wanted to see if he would actually do it. Also, this test will rule out any problems with system services.
Yeah, I may just end up doing that in the end. I had already done it with pointers, but I am finding some bugs in it and a few random crashes which I think might have to do with the garbage collector. I think I'll leave the pointers to my C++ code.
The easiest way to have a reference to a value type is to use boxing.

float a = 12;
object float_object = (object)a;

Now you have reference to a.

theTroll
Quote:Original post by TheTroll
The easiest way to have a reference to a value type is to use boxing.

float a = 12;
object float_object = (object)a;

Now you have reference to a.

theTroll


Modifying float_object will not affect a, but passing float_object to other functions which modify it will affect any other reference to float_object.

class boxed<T> where T : struct{   public T value;   public boxed(T v) { value = t; }}


The nature of value types is that they are always temporary in nature when you actually do things with them. Auto boxing won't help either, and dealing with objects as your data type is a very very bad idea.

The thing is,
if a method takes in an object, which is actually a boxed float, then you can't actually change it. You will be making a reference change, creating a new boxed float, not making a value change (as a ref float would).

This topic is closed to new replies.

Advertisement