View Single Post
  #2 (permalink)  
Old April 20th, 2007, 06:43 AM
mega mega is offline
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