views:

503

answers:

1

I'm fairly new to C# and .NET - I'm trying to get conversion to work from an integer to an enum. The conversion must be performable by ChangeType (outside of my demo below this is fixed as it's within the data binding framework) and from what I've read it should work with what I'm doing, but I get an exception and if I place breakpoints in the functions of my conversion class, nothing ever gets called.

Thanks in advance! -Matthew.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace csharptest
{
 class Program
 {

  [TypeConverter(typeof(UnitEnumConverter))]
  public enum LengthUnits
  {
   METRES,
   FEET
  };

  public class UnitEnumConverter : EnumConverter
  {
   public UnitEnumConverter(System.Type type)
    : base(type.GetType())
   {
   }

   public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
   {
    if (sourceType == typeof(Int64)) return true;

    return base.CanConvertFrom(context, sourceType);
   }

   public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
   {
    if (value is Int64)
    {
        return (LengthUnits)(Int64)value;
    }
    return base.ConvertFrom(context, culture, value);
   }
  }

  static void Main(string[] args)
  {
   LengthUnits units = new LengthUnits();

   long x = 1;

   units = (LengthUnits)System.Convert.ChangeType(x, typeof(LengthUnits));

  }
 }
}
+1  A: 

Starting over from previous answers

Convert.ChangeType will not bother looking at the TypeConverter so that is no help. Using Reflector to look at Convert.ChangeType it just looks like it will not work. It has a static map of things that it can convert to. If it isn't in that list it will not try and convert. This is funny because a straight cast of an int or a long to your enum will just work.

I'm not sure which binding framework you are using but it seems odd that it would go down this route for enums.

I'm sorry I couldn't be of more help.

Mike Two
Thanks - I've amended that, but unfortunately I'm still getting the InvalidCastException on the last (code) line :-(
Matthew Bernstein
Updated the answer. I only looked at the TypeConverter problem before. I hadn't looked at what you were really trying to do. Hopefully the new answer is more helpful.
Mike Two
Mike, thanks again for your help. Unfortunately, I'm not in control of how the conversion is done. I have a database which contains a field with a numerical representation of my enum value (the Int64) which is being databound to a property of type LengthUnits.If I do nothing, it simply doesn't work, if I add a Format handler to the binding, then I can handle things that way but would have to do so for every single control (and not doable at design time).All I really want is to get data binding to successfully work from a field of type Int32 to a property of type LengthUnits :-s
Matthew Bernstein
Should add - the exception that gets generated in the data binding stuff follows a call to ChangeType(), hence why I'm trying to get it working with that in my scaled-down example.
Matthew Bernstein
Thanks Mike - you've actually been a lot of help in that I now know I'm heading down a dead end on this route. I'm also puzzled as to why the databinding screws up given the cast works fine... will have to find some work around (maybe add a proxy property of type int that updates the other - bit messy but saves this agro!)Cheers,Matthew.
Matthew Bernstein