p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript (http://p2p.wrox.com/forumdisplay.php?f=85)
-   -   A quick way to parse a number to js enum (http://p2p.wrox.com/showthread.php?t=56632)

mega April 13th, 2007 02:03 AM

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:

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:

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!

This is a cross-post (http://p2p.wrox.com/topic.asp?TOPIC_ID=58984).

 - mega
Moving to C# .NET

mega April 20th, 2007 06:45 AM

Almost solved. See http://p2p.wrox.com/topic.asp?TOPIC_ID=58984

 - mega
Moving to C# .NET


All times are GMT -4. The time now is 02:05 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.