1

I have a enum like this.

public enum eTypeVar
{
    Int,
    Char,
    Float
};

and I want to convert it to type , to do something like this:

eTypeVar type=eTypeVar .Int;
string str= eTypeVar.ToString .ToLower();
(str as type) a=1;

How would I do it?

6
  • 1
    Where is the enum? What are you trying to achieve? Commented Oct 10, 2012 at 11:19
  • 3
    It might be better if you can explain the problem you're trying to solve instead of the solution you're trying to create. Commented Oct 10, 2012 at 11:19
  • i have enum that contains "int" "char" ec Commented Oct 10, 2012 at 11:19
  • i want to use in the enum for define and check values of variable Commented Oct 10, 2012 at 11:21
  • You are really going to need to articulate your question a bit better than this. Commented Oct 10, 2012 at 11:23

3 Answers 3

4

You can use Enum.Parse such as:

YourEnumType realValue = Enum.Parse(typeof(YourEnumType), "int");
Sign up to request clarification or add additional context in comments.

Comments

1

I think what you need is:

Dictionary<eTypeVar, Type> _Types = new Dictionary<eTypeVar, Type> {
    { eTypeVar.Int, typeof(Int32) },
    { eTypeVar.Char, typeof(Char) },
    { eTypeVar.Float, typeof(Single) }
};

public Boolean Check(eTypeVar type, Object value)
{
    return value.GetType() == _Types[type];
}

You cannot convert a variable into a type for another variable declaration. You should rethink your design. Anyway it doesn't make sence what you are doing. If you know that you want to use a int, why not declare it:

String name = "int";
int value = 1;

If you want to have dynamic code for some reasons you could use Reflection and a generic method.

public void DoSomething<T>(T value)
{
    ....
}

Then you can construct the method at runtime using reflection and invoke it. But at this time i think you need more basics of C# to work with this features.

5 Comments

i create a variable with this enum, and i want to check if the value suitable to his type
@HodayaShalom You cannot create a variable from a variable. Variable types are compile-time constants and not defined at runtime. For what purpose do you need your "dynamic" variable?
in my program the user can to create variable, for that
i need it only for checks only! not for define, how i know the type without to do switch with the options of the enum
@HodayaShalom Added something that should fit your needs.
0

you can try this... for example you have enum like this

  public enum Emloyee
 {
   None = 0,
  Manager = 1,
  Admin = 2,
  Operator = 3
}

then convert enum to this

Emloyee role = Emloyee.Manager;
int roleInterger = (int)role;

end for enum to string..

Emloyee role = Emloyee.Manager;
string roleString = role.ToString();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.