Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To 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
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 12th, 2007, 08:08 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:
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
  #2 (permalink)  
Old April 20th, 2007, 06:43 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

After spotting this article at quirksmode http://www.quirksmode.org/js/associative.html I've came up with this, which is almost what I want:
Code:
var Enum = {
    GetNames: function(enumtype){
        var names = [];
        for (var enumName in enumtype){
            names.push(enumName);
        }
        return names;
    },
    GetName: function(enumtype, enumvalue)
    {
        for (var enumName in enumtype){
            if(enumtype[enumName] == enumvalue)
                return enumName;
        }
    },
    GetValues: function(enumtype){
        var values = [];
        for (var enumName in enumtype){
            values.push(enumtype[enumName])
        }
        return values;
    },
    Parse: function(enumtype, enumvalue){
        for (var enumName in enumtype){
            if(enumtype[enumName] == enumvalue)
            {
                return eval(enumtype + "." + enumName);
            }
        }
    }
}
I can't get the Parse function to work though. I receive an missing ] after element list in Firebug where I return the enum.

 - mega
Moving to C# .NET
Reply With Quote
  #3 (permalink)  
Old April 20th, 2007, 07:59 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Can't see why you get that error, but what is Parse supposed to do?

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #4 (permalink)  
Old April 20th, 2007, 01:45 PM
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

Hi Joe.

For a minute I thought this was too niche for people at p2p to care. ;)
The Parse function is suppose to return the supplied type of enum. Given the enum:
Code:
var myEnum = { West : 1, East: 2, North : 4, South : 8 }
I would use the Parse function as
Code:
var map = { direction: Enum.Parse(myEnum, 4) }
It's practical when you have data from e.g. XML web services ect.
I did however come up with a small alteration that doesn't give me a syntax error.
Code:
Parse: function(enumtype, enumvalue){
    for (var enumName in enumtype){
        if(enumtype[enumName] == enumvalue)
        {
            return eval([enumtype + "." + enumName]);
        }
    }
}
But it returns [object Object].North
The idea comes from the .NET Enum class.

 - mega
Moving to C# .NET
Reply With Quote
  #5 (permalink)  
Old April 21st, 2007, 03:56 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

But in C# the Parse method returns a typed representation of the Enum given its value or name.
So if you want to supply 4 and get back an object that has one property, West that has a value of 4 then do:
Code:
Parse: function(enumtype, enumvalue){
    for (var enumName in enumtype){
        if(enumtype[enumName] == enumvalue)
        {
            var o = {};
            o[enumName] = enumvalue;
            return o;
        }
    }
}
--

Joe (Microsoft MVP - XML)
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 1 April 20th, 2007 06:45 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 05:26 AM.


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