I'm looking for some help of converting a long to a type enum in something that would be similar to this:

public object ToActionType(Long myval)
{
return (object)((ActionTypes)myval)
}

:sad: but the trick is that I don't know the enum just the type of the enum...(I also know for a fact that myval is always part of the enum type t.)

public object ToEnum(Long myval,Type t)
{
return ?????
}

Could somebody explain how to achieve that? :o

I am not sure why you want to return an object instead of the ActionValue type but you want

public object ToActionType(Long myval)
{
int myIntVal = (int)myval;
ActionTypes myActionType = (ActionTypes) myIntVal;
return (object) myActionType;
}

The answer I was looking for was the static function of class Enum...
Enum.ToObject(t,myval)
As specified in the email I didn't know the enum type at the time of call otherwise my first function work perfectly and I wouldn't have a function in the first place.
Thanks anyway for trying to help much appreciated.

Can you use the string value of the enum instead of the long? You say that you know that myVal is always part of the enum t. so i am assuming you are storing it somewhere. If you could get the string value of the enum for myVal instead then it is a simple case of calling Enum.Parse(t, myValString) which returns an object. Does that help?

if you dont have the string value then you try
string myValString = Enum.GetName(t, myVal);
object theObject = Enum.Parse(t, myValString);

Please ensure you use the Enum not enum.

Hope it helps

This article has been dead for over six months. Start a new discussion instead.