Jump to content

  • Log In with Google      Sign In   
  • Create Account


c# assigning new int to void*?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Solid_Spy   Members   -  Reputation: 400

Like
0Likes
Like

Posted 29 April 2014 - 08:32 PM

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*?



Sponsor:

#2 fastcall22   Crossbones+   -  Reputation: 4115

Like
4Likes
Like

Posted 29 April 2014 - 09:51 PM

By boxing it:
 
Object val = Int32.Parse(c);

Edited by fastcall22, 29 April 2014 - 09:53 PM.

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#3 ChaosEngine   Crossbones+   -  Reputation: 2220

Like
3Likes
Like

Posted 29 April 2014 - 10:17 PM

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)

Edited by ChaosEngine, 29 April 2014 - 10:20 PM.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#4 Nypyren   Crossbones+   -  Reputation: 3939

Like
3Likes
Like

Posted 29 April 2014 - 10:22 PM

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.

#5 Pink Horror   Members   -  Reputation: 1115

Like
2Likes
Like

Posted 29 April 2014 - 10:32 PM

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#.

#6 nfactorial   Members   -  Reputation: 726

Like
2Likes
Like

Posted 30 April 2014 - 01:18 AM

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!



#7 Solid_Spy   Members   -  Reputation: 400

Like
0Likes
Like

Posted 30 April 2014 - 12:14 PM

Alright, thank you for the answers everyone :3

 

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS