p2p.wrox.com Forums

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

mega April 12th, 2007 08:08 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:
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 April 20th, 2007 06:43 AM

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

joefawcett April 20th, 2007 07:59 AM

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

--

Joe (Microsoft MVP - XML)

mega April 20th, 2007 01:45 PM

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

joefawcett April 21st, 2007 03:56 AM

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)


All times are GMT -4. The time now is 06:18 AM.

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