View Single Post
  #1 (permalink)  
Old April 12th, 2007, 08:08 AM
mega mega is offline
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen N, , Denmark.
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default A quick way to parse a number to js enum

Hi all!

I'm having a subtle problem with enumerations in javascript. Let me first outline the problem.
I have an enumeration in javascript (using JSON) that look like this:
Code:
var EventsToShow = {"MyEvents" : 1, "AllEvents" : 2 };
Now this is all good but when I want to parse a number (ie. 1 or 2) to an EventsToShow enum I have to use a switch statement (which is always at least 5 lines of code) like so:
Code:
switch([1 or 2])
{
  case EventsToShow.MyEvents:
    alert("This is an EventsToShow.MyEvents");
    break;
  case EventsToShow.AllEvents:
    alert("This is an EventsToShow.AllEvents");
    break;
}
My question is: Is there a simple way to parse an Enum to a number (integer) and vice versa, perhaps using eval?

Cheers, Jon!

 - mega
Moving to C# .NET
__________________
- mega
Aspiring JavaScript Ninja
Reply With Quote