Search code examples
c#xna

Reference to number types in C#?


I was trying to create a Tween class for general purpose use in XNA games, and I ended up stuck with a problem.

In order to be able to automatize the process the way I want to, I need to be able to store a reference to a numerical value in another class so I can modify it with each update cycle.

Is there any way to accomplish that without resorting to unsafe code / pointers?


Solution

  • Use a generic wrapper class, and pass that around instead. When the held property is modified, all objects with a reference to the wrapper will automatically get the new value.

    public class Wrapper<T>
    {
        public T Value {get; set;}
    }
    

    Used as:

    Wrapper<double> myDoubleObject = new Wrapper<double>();
    myDoubleObject.Value = 10;