c# assigning new int to void*?

Started by
5 comments, last by SolDirix 9 years, 11 months ago

Hello, I am currently developing a level editor, and I am having some problems.

Firstly, I want to create different game objects to use in my level editor and, assign them arbitrary variables loaded from a file. I tried using a void* for this.

However, the variable that was of type int, I tried to assign to the void* did not seem to work, I tried looking up online on how to add a new int to a void*, but found no results :(


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    class ObjectVariable
    {
        public ObjectVariable(uint a, string b, string c, string d)
        {
            variableType = a;
            variableName = b;

            switch (a)
            {
                //Can't do this :(.
                case 0: variableValue = new int(System.Convert.ToInt32(c));
                    break;
            }
            variableDescription = c;
        }
        public uint variableType;
        public string variableName;
        public void* variableValue;
        public string variableDescription;
    }
}

So, How do I assign a new int to a void*?

View my game dev blog here!

Advertisement
By boxing it:

Object val = Int32.Parse(c);

Another option would be to make your ObjectVariable class generic


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    class ObjectVariable<TValue>
    {
        public ObjectVariable(string name, string description, TValue value)
        {
            Name = name;
            Value = value;
            Description = description;
        }
    
       // public uint variableType; not needed anymore
        public string Name {get;set;} // don't smurf name!
        public TValue Value {get;set;}
        public string Description {get;set;}
    }
}

As a general rule, you shouldn't see a '*' in C# code unless you're

  • multiplying numbers
  • using unsafe (i.e. you really know what you're doing)
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Like fastcall says, to make a variable that can hold a value of any type, use the 'object' type. void* (and pointers) are things you should never* need to use in C#.

You can use the "is" and "as" keywords on an object to figure out what type it is and convert it back to its original type - this might let you get away without your variableType field.



*There are extremely rare cases where you should use pointers, and you will probably be a C# master by the time you run into a case where you need one.

However, the variable that was of type int, I tried to assign to the void* did not seem to work, I tried looking up online on how to add a new int to a void*, but found no results


1. You should probably say how it didn't work? Does it compile? Does it crash? Does it just do something bad? And whatever happens, if there were any sort of error messages given to you, those are very helpful.

2. I don't see why you have decided to go with using unsafe C# code just to hold onto some generic property value. Are you aware of the "object" type? A regular old object makes more sense to me in this situation that any sort of pointer in C#.

The simple answer is you don't, and you shouldn't. Even in C++ you shouldn't do it this way, though you can.

There are many alternatives:

As someone else mentioned you can make your class a generic, and have the type declared as part of your class. One issue you will get then is that you can't simply make a collection of your objects. To solve that you can use inheritance, by having an ObjectVariableBase which has everything but the value. Then derive a generic from that with the actual variable type. If you want methods specific to the type, then deriving a specialised implementation is also an option (ObjecVariableInt, ObjectVariableString etc).

Another option in C# is to use a dynamic method to access the value. However, I feel that's a little advanced for what you're trying to do here.

You shouldn't give single letter variable names in the constructor, give them proper names. As ChaosEngine shows in his code, call them 'name', 'description', 'value' or whatever your preference is, just not 'a', 'b' and 'c'.

n!

Alright, thank you for the answers everyone :3

I've decided to use the Object variable type with variable classes. Thank you!

View my game dev blog here!

This topic is closed to new replies.

Advertisement