Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Wrong enum values displayed in Indicators dialog

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

    Wrong enum values displayed in Indicators dialog

    Consider the following enum:
    Code:
        public enum DemoEnum {
            [EnumDisplayName("User-friendly label for Value 1")]        value1,
            [EnumDisplayName("User-friendly label for Value 2")]        value2
        }
    When using that as the basis for an enum property:
    • NinjaTrader 7 gets it right -- in the Indicators dialog it displays the EnumDisplayName for each enum value.
    • NinjaTrader 8 is broken -- it just displays the unfriendly programmer-ese (e.g. "value1", "value2").

    --EV

    #2
    Originally posted by ETFVoyageur View Post
    Consider the following enum:
    Code:
        public enum DemoEnum {
            [EnumDisplayName("User-friendly label for Value 1")]        value1,
            [EnumDisplayName("User-friendly label for Value 2")]        value2
        }
    When using that as the basis for an enum property:
    • NinjaTrader 7 gets it right -- in the Indicators dialog it displays the EnumDisplayName for each enum value.
    • NinjaTrader 8 is broken -- it just displays the unfriendly programmer-ese (e.g. "value1", "value2").

    --EV
    Looks like a custom Attribute? You may have to rewrite your code implementation.

    Comment


      #3
      Originally posted by koganam View Post
      Looks like a custom Attribute? You may have to rewrite your code implementation.
      What re-writing would you suggest?

      The attribute is a custom one, but that does not matter. I could also have used a more standard attribute (say "Description"). What is important is that I provide a type converter for the property. The bug is that NT7 honors type converters, but NT8 does not.. NT 8 should honor type converters, because there is no other way to get user-friendly labeling. Forcing programmer-ese labeling for enum properties is pretty ugly.

      --EV

      Comment


        #4
        Originally posted by ETFVoyageur View Post
        What re-writing would you suggest?

        The attribute is a custom one, but that does not matter. I could also have used a more standard attribute (say "Description"). What is important is that I provide a type converter for the property. The bug is that NT7 honors type converters, but NT8 does not.. NT 8 should honor type converters, because there is no other way to get user-friendly labeling. Forcing programmer-ese labeling for enum properties is pretty ugly.

        --EV
        NT8 does honor TypeConverters, at least as far as I can see, both standard ones that come with .NET and custom ones.

        Without seeing your implementation, it would be hard to say what you might need to change/modify in your code.

        Comment


          #5
          Originally posted by koganam View Post
          NT8 does honor TypeConverters, at least as far as I can see, both standard ones that come with .NET and custom ones.

          Without seeing your implementation, it would be hard to say what you might need to change/modify in your code.
          Specifically, are you assuring me that NT 8 does honor enum value type converters for properties that use that enum? (I just want to be sure you are not saying that NT 8 would honor a type converter for the property, which would be nice, but not the same thing.)

          If that is what you are saying I'll dig in and find the problem. Seems odd that my code works with NT 7 though -- I would have thought that a type converter would be pretty binary -- either it works or it does not work.

          --EV

          Comment


            #6
            Originally posted by ETFVoyageur View Post
            Specifically, are you assuring me that NT 8 does honor enum value type converters for properties that use that enum? (I just want to be sure you are not saying that NT 8 would honor a type converter for the property, which would be nice, but not the same thing.)

            If that is what you are saying I'll dig in and find the problem. Seems odd that my code works with NT 7 though -- I would have thought that a type converter would be pretty binary -- either it works or it does not work.

            --EV
            TypeConverters work the same in all .NET versions that are supported, so technically "yes".
            Last edited by koganam; 09-25-2016, 08:51 PM.

            Comment


              #7
              Originally posted by koganam View Post
              TypeConverters work the same in all .NET versions that are supported, so technically "yes". The question though, is: "Are you simply looking for a user-friendly display of the enum members in the PropertyGrid?" If so, a type converter seems to be overkill. A simple Custom Attribute defined to expose the friendly text should be fine.
              I do not understand your comment about a simple Custom Attribute.

              I also am still puzzled about what I am doing wrong as is -- you say type conversion works for enum properties in NT 8, but it is not working for me. I gather it is working for you, or else you would not have said it is working.

              --EV

              Comment


                #8
                Originally posted by ETFVoyageur View Post
                I do not understand your comment about a simple Custom Attribute.

                I also am still puzzled about what I am doing wrong as is -- you say type conversion works for enum properties in NT 8, but it is not working for me. I gather it is working for you, or else you would not have said it is working.

                --EV
                Let me give you a somewhat oversimple example.
                Code:
                [AttributeUsage(AttributeTargets.Field)]
                public class EnumFriendlyNameAttribute : Attribute //or even [I]DisplayNameAttribute[/I]
                {
                    private string _name;
                    public string Name
                    {
                        get{ return _name; }
                        set{ _name = value; }
                    }
                }
                And we would use it like so:
                Code:
                enum OrderStatus
                {
                    [EnumFriendlyName(Name="New Order")]
                    NewOrder = 1,
                    [EnumFriendlyName(Name="In Process")]
                    Processing = 2,
                    [EnumFriendlyName(Name="Shipped")]
                    Shipped = 3
                }
                Here is another way:
                Code:
                [AttributeUsage(AttributeTargets.Field)]
                public class EnumDisplayNameAttribute : DisplayNameAttribute
                {
                    public EnumDisplayNameAttribute()
                        : base()
                    {
                 
                    }
                 
                    public EnumDisplayNameAttribute(string displayName)
                        : base(displayName)
                    {
                 
                    }
                }
                Used like so:
                Code:
                public enum RecordStatus
                {
                	New = 0,
                 
                	Error = 1,
                 
                	[EnumDisplayName("Prior Hours")]
                	PriorHours = 2,
                 
                	[EnumDisplayName("Normal Hours")]
                	NormalHours = 4,
                 
                	[EnumDisplayName("Excess Hours")]
                	ExcessHours = 8,
                }
                Both are just Custom Attributes with no TypeConverter.

                Of course, if you wanted to process the property, such as show the DisplayName when ToString() is called, you will probably have to write an overload to ToString().

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by CortexZenUSA, Today, 12:53 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by CortexZenUSA, Today, 12:46 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by usazencortex, Today, 12:43 AM
                0 responses
                5 views
                0 likes
                Last Post usazencortex  
                Started by sidlercom80, 10-28-2023, 08:49 AM
                168 responses
                2,265 views
                0 likes
                Last Post sidlercom80  
                Started by Barry Milan, Yesterday, 10:35 PM
                3 responses
                11 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X