locked
Type converter - Convert from string to type RRS feed

  • Question

  • User712911596 posted

    I have a custom type converter on a class that is working fine on the "ToString()". I have the reverse coded into the CanConvertFrom(), but I can't seem to find a way to create the object from a String. Am I missing something here?

    Monday, June 30, 2008 10:29 AM

Answers

  • User-252544907 posted

    Simply override the ConvertFrom method on your TypeConverter class. So take its input string value, process it, and have the ConvertFrom method instantiate and return an instance of your class.

    Hope that helps,
    Wim

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 30, 2008 12:40 PM

All replies

  • User-252544907 posted

    Simply override the ConvertFrom method on your TypeConverter class. So take its input string value, process it, and have the ConvertFrom method instantiate and return an instance of your class.

    Hope that helps,
    Wim

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 30, 2008 12:40 PM
  • User712911596 posted

            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
                if (value == null) { return new Corner(); }

                if (value is String) {
                    String[] vals = ((String)value).Split(' ');
                    return new Corner(
                        (CornerSizes)Enum.Parse(typeof(CornerSizes), vals[0], true)
                        , (CornerTypes)Enum.Parse(typeof(CornerTypes), vals[1], true)
                    );
                }

                return base.ConvertFrom(context, culture, value);
            }

     The above is defined in the Converter...I eventually found a way around my issue (by not doing the conversion at all), but I'm still interested in a answer.
     

    Thursday, July 3, 2008 9:34 AM
  • User-252544907 posted

    Can you show an example of the ToString() on your Corner object?

    Thanks,
    Wim

    Thursday, July 3, 2008 11:58 AM
  • User712911596 posted

    Sorry for the slow response...


            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
                if (value == null) { return new Corner(); }

                if (value is String) {
                    String[] vals = ((String)value).Split(' ');
                    return new Corner(
                        (CornerSizes)Enum.Parse(typeof(CornerSizes), vals[0], true)
                        , (CornerTypes)Enum.Parse(typeof(CornerTypes), vals[1], true)
                    );
                }

                return base.ConvertFrom(context, culture, value);
            }

            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
                if (destinationType == typeof(String)) {
                    Corner c = (Corner)value;
                    return String.Format(
                        "{0} {1}"
                        , c.Size.ToString()
                        , c.Type.ToString()
                    );
                }
                return base.ConvertTo(context, culture, value, destinationType);
            }

     

    I couldn't find a way to call the ConvertFrom in my code, I was using the "defualt" ToString() method that is always available to get the Object to a string (and assuming it was using my logic in the ConvertTo() somehow). This is where I am confused.

    Wednesday, July 9, 2008 2:37 PM
  • User-252544907 posted

    Darren,

    If you want to explicitly call ConvertFrom on your typedescriptor, use the following:

    Corner c = (Corner)TypeDescriptor.GetConverter(typeof(Corner)).ConvertFromString("30 solid");

    That will subsequently call your overridden ConvertFrom method in your custom CornerTypeConverter class.

    Does that help?

    Cheers,
    Wim

    Wednesday, July 9, 2008 4:16 PM