Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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
Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:34 PM.


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