4

Is there a way to share an enum between Code behind .Net (VB or C#) and javascript so they both use the same values?

can we register an enum into the clientScript..?

Edit: The question is: Is there a way to share a variable that act like an enum in .Net and do the same job in javascript. I could declare both in javascript and .Net but if someone modify the code later and juste change it in 1 place, everything will blow up... This is why I am looking for somehting "shared"

Thanks

5
  • 1
    Javascript doesn't have enumerations as such. What do you mean, exactly? Commented Aug 2, 2012 at 15:06
  • yes they do? misfitgeek.com/2011/10/using-enum-in-javascript Commented Aug 2, 2012 at 15:07
  • From the article you just linked to: "There is no actual ENUM type in JavaScript but there are was that we can get close.". Those are all normal Javascript objects. Commented Aug 2, 2012 at 15:09
  • it's not called "enum" but it do exactly the same job. So the question is: can we share a variable that could act as an enum in .Net and do the same job in javascript Commented Aug 2, 2012 at 15:11
  • 2
    I don't think there's anything inbuilt, but you could probably write an extension method to convert your enum into javascript form as a string and then register that script on your pages. Commented Aug 2, 2012 at 15:11

3 Answers 3

4
//The type of enum we want to use
var enm = typeof(AcceptRejectRule); //or any other enum

//Create the javascript string
var str = "var " + enm.Name + " = {\n" +
        String.Join(",\n", 
               Enum.GetValues(enm).Cast<object>()
               .Select (o => Enum.GetName(enm, o) + ": " + ((int)o))) +
        "\n};";

//Register the javascript to the page for rendering     
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), enm.Name, str, true);
Sign up to request clarification or add additional context in comments.

1 Comment

i'll test it before put my vote but it look like what i was searching for. Thanks
1

Yes, you can have a .NET enum mirrored in javascript:

C#:

enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

JS:

var Days = {
    Sat: 0,
    ...
    Fri: 6
};

Note that this is just an object named Days with properties that have numeric values. There is no language implementation of an Enum in javascript.

If you want something to automagically convert your C# enum to javascript, just use GetValues to iterate over the C# enum and inject it as a javascript object declaration. It boils down to string concatenation, there is nothing in the .NET framework to do this for you.

3 Comments

The question is how to share one enumeration between the languages, not how to express it in both.
@Oded what does "share an enum" mean?
I believe the OP wants to have the enum defined in .NET and generate and use a javascript equivalent from it.
0

Already answered but thought I would share my solution as well:

public enum UpdateOptions
{
    Create = 0,
    Read,
    Update,
    Delete,
    None
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(ToJSLiteral<UpdateOptions>());

        Console.ReadKey();
    }

    static string ToJSLiteral<T>() where T : struct
    {
        Type type = typeof(T);
        StringBuilder sb = new StringBuilder();
        int[] values = Enum.GetValues(type).Cast<int>().ToArray();
        sb.Append("var " + type.Name + " = {\n");

        foreach(int value in values)
        {
            string name = Enum.GetName(type, value);
            sb.Append(name + " : " + value + ",\n");
        }

        sb.Length -= 2;
        sb.Append("\n};");
        return sb.ToString();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.