User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 426,568 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,651 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 2499 | Replies: 3
Reply
Join Date: Jan 2006
Posts: 2
Reputation: Sylvain Bouche is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sylvain Bouche Sylvain Bouche is offline Offline
Newbie Poster

Help Converting a long to an Enum Value

  #1  
Jan 6th, 2006
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)
}

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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2006
Location: Its the internet... i am everywhere lol
Posts: 274
Reputation: f1 fan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 10
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Converting a long to an Enum Value

  #2  
Jan 11th, 2006
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;
}
Reply With Quote  
Join Date: Jan 2006
Posts: 2
Reputation: Sylvain Bouche is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sylvain Bouche Sylvain Bouche is offline Offline
Newbie Poster

Re: Converting a long to an Enum Value

  #3  
Jan 12th, 2006
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.
Reply With Quote  
Join Date: Jan 2006
Location: Its the internet... i am everywhere lol
Posts: 274
Reputation: f1 fan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 10
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Converting a long to an Enum Value

  #4  
Jan 12th, 2006
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 9:32 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC