Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
| Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 13th, 2007, 02:03 AM
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:

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
Aspiring JavaScript Ninja
  #2 (permalink)  
Old April 20th, 2007, 06:45 AM
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

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

 - mega
Moving to C# .NET


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enum gets initalized to first value mhshpk ASP.NET 2.0 Professional 2 September 4th, 2007 10:16 AM
A quick way to parse a number to js enum mega Javascript How-To 4 April 21st, 2007 03:56 AM
Enum in VB saeta119 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 June 7th, 2003 04:10 PM
Using Enum Type Mike2000 VB.NET 2002/2003 Basics 2 June 5th, 2003 05:20 AM





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