Announcement

Collapse
No announcement yet.

Assigning string values to enums?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Assigning string values to enums?

    Saltarelle had the possibility to make convert enums to string values in method declarations.

    Is there a possibility to do the same in brdige.net?

    #2
    Can you demonstrate a code which is valid in Saltarelle but you cannot use it in Bridge? Thanks

    Comment


      #3
      You mean I can just use this like it is in Saltarelle? https://github.com/Saltarelle/Saltar...et/HttpVerb.cs
      Last edited by txdv; 2015-08-17 @ 10:01 PM.

      Comment


        #4
        Yes, please see the following sample
        using System;
        using Bridge;
        using Bridge.Html5;
        
        namespace BridgeTest
        {
            [Enum(Emit.StringNameLowerCase)]
            [Ignore]
            public enum HttpVerb
            {
                [Name("GET")]
                Get = 0,
                [Name("POST")]
                Post = 1,
                [Name("PUT")]
                Put = 2,
                [Name("DELETE")]
                Delete = 3,
                [Name("HEAD")]
                Head = 4
            }
        
            public class App
            {
                [Ready]
                public static void Main()
                {
                    var verb = HttpVerb.Post;
        
                    switch (verb)
                    {
                        case HttpVerb.Get:
                        case HttpVerb.Post:
                        case HttpVerb.Put:
                        case HttpVerb.Delete:
                        case HttpVerb.Head:
                            Console.Log(verb);
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
            }
        }

        Comment


          #5
          If you just specify Emit.StringNameUpperCase, you don't have to specify the Name on a per enum field basis.

          Comment


            #6
            Yes, Enum attribute has several modes
            - Name - emit as reference (for example, BridgeTest.HttpVerb.Get)
            - Value - emit as value (inline value), for example Get will be emitted as 0
            - StringName - emit as string with lower first symbol, for example 'get'
            - StringNamePreserveCase - emit as string with preserving case, for example 'Get'
            - StringNameLowerCase - emit as lower case string, for example 'get'
            - StringNameUpperCase - emit as upper case string, for example 'GET'

            Name can be overriden by using Name attribute

            Comment

            Working...
            X