Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enum conversion question

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

    Enum conversion question

    This is basically a continuation of a year-old thread that never got resolved before I had to do other things. The basic issue is that I want to show user-friendly names for enum values in the Indicators property grid. There may be an easier way, and I'd be interested. I'm also interested in why I am having trouble with the usual advice.
    • Create a custom property -- Enum DisplayNameAttribute
    • Create a custom type converter for the property -- EnumDisplayNameConverter
    • Decorate the enum values with the custom property
    • Decorate the enum property with a type converter -- [TypeConverter(typeof(EnumDisplayNameConverter))]

    I have done these and it does not work. It did work for me in V7, but I have been unable to make it work in V8 -- presumably my problem, not a V8 bug, but I need to understand it. I put breakpoints in all of the type converter functions and discovered that the type converter constructor is getting called for each appropriate enum, but no type converter methods are ever called, and hence the user-friendly name is never shown.

    Custom property:
    Code:
        [AttributeUsage(AttributeTargets.Field)]
        public class EnumDisplayNameAttribute : DisplayNameAttribute {
            public EnumDisplayNameAttribute ( string s ) : base(s) { Name = s; }
            public string Name { get; private set; }
        }
    Custom type converter:
    Code:
        class EnumDisplayNameConverter : EnumConverter
        {
            private Type _enumType;
            
            public EnumDisplayNameConverter(Type type) : base(type)
                { _enumType = type; }
                
            public override bool CanConvertTo(ITypeDescriptorContext context,Type destType)
                { return destType == typeof(string); }
                
            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
            {
                System.Reflection.FieldInfo field = _enumType.GetField(Enum.GetName(_enumType, value));
                EnumDisplayNameAttribute attr = Attribute.GetCustomAttribute(field, typeof(EnumDisplayNameAttribute)) as EnumDisplayNameAttribute; 
                return (attr != null) ? attr.Name : value.ToString();    // Could also return string.Empty in the null case
            }
            
            public override bool CanConvertFrom(ITypeDescriptorContext context,Type srcType)
                { return srcType == typeof(string); } 
                
            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                foreach (System.Reflection.FieldInfo fi in _enumType.GetFields()) {
                    EnumDisplayNameAttribute attr = Attribute.GetCustomAttribute(fi, typeof(EnumDisplayNameAttribute)) as EnumDisplayNameAttribute; 
                    if ((attr != null) && ((string)value == attr.Name))
                    return Enum.Parse(_enumType, fi.Name);
                }
                return Enum.Parse(_enumType, (string)value);
            }
        }
    Decorated enum:
    Code:
        public enum SmoothType
        {
            [EnumDisplayName("Single EMA")]        SingleEMA,
            [EnumDisplayName("Double EMA")]        DoubleEMA,
            [EnumDisplayName("Triple EMA")]        TripleEMA,
            [EnumDisplayName("No smoothing")]    None
        }
    Property trying to use the type converter:
    Code:
            private SmoothType valueSmoothType = SmoothType.None;
            [DefaultValue(SmoothType.None)]
            [RefreshProperties(RefreshProperties.All)]
            [TypeConverter(typeof(EnumDisplayNameConverter))]
            [Browsable(true)]
            [NinjaScriptProperty]                           // So the property can be used from NinjaScript (e.g. another indicator or a Strategy)
            [Display(Order = indicatorParameterBase + 0, Name = "Value line smoothing", GroupName = indicatorGridCategory,
                    Description = "This property controls whether or not to apply smoothing to the indicator value plot")]
            public SmoothType ValueSmoothType
            {
                get
                {
                    PrintGetEntry("returning " + valueSmoothType.ToString());
                    return valueSmoothType;
                }
                set
                {
                    PrintSetEntry("old value=" + valueSmoothType.ToString() + "  new value=" + value.ToString());
                    valueSmoothType = value;
                }
            }
    It's hard to believe that this worked in V7, and fails in V8. Furthermore, Koganam, for whom I have great respect, assures me (in the cited thread) that it does work in V8. Any comments as to what I am doing wrong? Why is the type converter getting constructed, but then none of its methods are ever called?

    Thanks,
    EV
    Last edited by ETFVoyageur; 07-29-2016, 11:09 PM.

    #2
    I am not positive this is a concept NT8 currently supports, and I am checking with our developers to give you an answer (along with your other thread)

    MatthewNinjaTrader Product Management

    Comment


      #3
      Thanks, Matthew. As with the other question, if not supported, then how should I achieve the objective -- user-friendly enum names in the GUI.

      FWIW: with both of these issues -- this one and the type conversion one -- I did not invent them. They are the answers you get if you go on the Internet to find how to solve those problems. Also, they worked in V7.

      --EV
      Last edited by ETFVoyageur; 08-01-2016, 07:48 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by andrewtrades, Today, 04:57 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by chbruno, Today, 04:10 PM
      0 responses
      6 views
      0 likes
      Last Post chbruno
      by chbruno
       
      Started by josh18955, 03-25-2023, 11:16 AM
      6 responses
      436 views
      0 likes
      Last Post Delerium  
      Started by FAQtrader, Today, 03:35 PM
      0 responses
      7 views
      0 likes
      Last Post FAQtrader  
      Started by rocketman7, Today, 09:41 AM
      5 responses
      19 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X