Skip to main content
gamedev.net
🔒 Locked

Dators in C#

Started by luke2 7 replies 1k views
Original Post
luke2
luke2
Well, it was easy in C++.. for example I would do this:

class Dator
{
   private:
       TemplatedType* data; 
   public:
       Data( TemplatedType t )
       {
           data = &t 
       }
       void setData( TemplatedType t )
       {
            (*data) = t; //effects the entire program. 
       }
}
(that isn't a complete/correct class, but you get the idea.) So I was trying to do this in C#, and I can't get it to work.

class Dator
    {
        private Object data;

        public Dator( ref Object obj )
        {
            data = obj; 
        }

        public Object Data
        {
            get { return data; }
            set { data = value; }
        }
    }
I can't get changes that are placed upon data to manifest themselfs in the rest of the program- basically what I want is this in the client code:

Dator d = new Data( myInt ); //myInt = 42
d.Data = 22; 
System.Console.Write( myInt ); //prints 42 instead of 22.
Now, objects (classes) are supposedly reference types- but that doesn't seem to be the case here...
CipherCraft
CipherCraft
Try changing
private Object data;

to
private ref Object data;


In the ctor you're making a copy of the reffed object. You should probably change the attribute in the same way.

hth,
CipherCraft
Omid Ghavami
Omid Ghavami
As far as I know you can't keep references to value-types in C#. When you assign a value-type, such as an integer, to a variable of type Object the compiler will do something called boxing. As you can see there is no connection between the newly created Object and the struct instance it was created from.
What you could do is create wrapper classes around the value-types you'd like to use and use those instead.

EDIT:
Quote:
Original post by CipherCraft
Try changing
private Object data;

to
private ref Object data;


In the ctor you're making a copy of the reffed object. You should probably change the attribute in the same way.

hth,
CipherCraft


That's not valid C# code.
Best regards, Omid
snk_kid
snk_kid
As already mentioned the problem (it's not really a problem) is autoboxing however what you're doing is a bit silly since that Dator class is pretty much pointless because your trying to achieve something that already exists as part of the language core and .NET platform i.e. value types are objects (there all derived from System.Object), boxing/unboxing from/to value-types to/from objects, and nullable value-types.
luke2
luke2
Thats strange, I just changed some of the client code, and it works... I guess I must've been causing some unintended auto-boxing (I guess any primitives I pass in will actually need to be their class equivalents).

Thanks for helping guys!
smitty1276
smitty1276
What is the Dator class supposed to do?

In other words, what's wrong with:

int x = 42;
x = 22;
System.Console.Write(x);
Kambiz
Kambiz
using System;using System.Collections.Generic;using System.Text;using System.Reflection;namespace testgdnet{    class Dator    {        FieldInfo fi;        object obj;        public Dator(object o,string name)        {                        fi = o.GetType().GetField(name);            obj = o;            Console.WriteLine(fi.GetValue(o));        }        public Object Data        {            get { return fi.GetValue(obj); }            set { fi.SetValue(obj,value); }        }        public override string ToString()        {            return Data.ToString();        }    }    class Program    {        public int myInt;        static void Main(string[] args)        {            Program p = new Program();            p.Run();        }        void Run()        {            myInt = 42;            Dator d = new Dator(this,"myInt");            d.Data = 22;            Console.WriteLine(myInt); //writes 22        }    }}
luke2
luke2
That is certainly one way to do it, using reflection.. I will consider that.

Why am I trying to make a 'redundant' Dator class?
I am trying to make an ingame console (eg, press '`' and you can type in certain commands). However, I want to be able to change ingame values for debugging purposes, in fact that reflection idea is looking like a pretty good one.

Although I doubt it would affect my game adversely, is reflection all that expensive?

It occurs to me that I can probably detect if the object that owns that particular dator's data has been deleted --- I think I will do the reflection idea.
smitty1276
smitty1276
If you are doing tons of reflection, it could become significant. If you just need to find a class member by name when someone enters a particular command in a console, it is absolutely negligible.

Topic Locked

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

Sign in to reply to this topic.