Object property with different return types

Discussion in 'ASP .Net Building Controls' started by Simbad, Jan 12, 2004.

  1. Simbad

    Simbad Guest

    I have a control 'MyControl' with a property of
    type 'MyObject'. MyObject has a property 'MyField' that
    is a enum type. I want the type of enum to vary
    depending on a another property of MyObject
    called 'MyFieldType'. E.g. if MyFieldType = TypeA,
    MyField returns EnumA. See code below:

    public enum FieldTypes
    {
    TypeA,
    TypeB
    }

    public enum EnumA
    {
    Big,
    Medium,
    Small
    }

    public enum EnumB
    {
    Short,
    Tall,
    Squat,
    Huge
    }

    [DefaultProperty("MyFieldType")]
    public class MyObject
    {
    private FieldTypes m_MyFieldType;
    private object m_MyField = 0;

    public FieldTypes MyFieldType
    {
    get {return m_MyFieldType;}
    set {m_MyFieldType = value;}
    }

    public object MyField
    {
    get
    {
    if (MyFieldType == FieldTypes.TypeA)
    {
    return (EnumA)m_MyField;
    }
    else
    {
    return (EnumB)m_MyField;
    }
    }
    set
    {
    m_MyField = value;
    }
    }
    }


    My problem is that the MyField property is not editable
    via a dropdown list in the designer. It appears as a
    greyed-out (read only) string representation of the
    respective enum (EnumA or EnumB). I've tried writing a
    TypeConverter (inheriting from EnumConverter) but just
    ended up going round in circles.

    Does anyone have an example of what I'm trying to do or
    point me in the right direction?

    Thanks
     
    Simbad, Jan 12, 2004
    #1
    1. Advertising

  2. Simbad

    Teemu Keiski Guest

    Hi,

    I suspect that the property should be of only one type for it to work
    correctly. You can't parameterize its type (the type needs to be fixed at
    design-time).

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist


    "Simbad" <> wrote in message
    news:094a01c3d918$73f0a8a0$...
    >I have a control 'MyControl' with a property of
    > type 'MyObject'. MyObject has a property 'MyField' that
    > is a enum type. I want the type of enum to vary
    > depending on a another property of MyObject
    > called 'MyFieldType'. E.g. if MyFieldType = TypeA,
    > MyField returns EnumA. See code below:
    >
    > public enum FieldTypes
    > {
    > TypeA,
    > TypeB
    > }
    >
    > public enum EnumA
    > {
    > Big,
    > Medium,
    > Small
    > }
    >
    > public enum EnumB
    > {
    > Short,
    > Tall,
    > Squat,
    > Huge
    > }
    >
    > [DefaultProperty("MyFieldType")]
    > public class MyObject
    > {
    > private FieldTypes m_MyFieldType;
    > private object m_MyField = 0;
    >
    > public FieldTypes MyFieldType
    > {
    > get {return m_MyFieldType;}
    > set {m_MyFieldType = value;}
    > }
    >
    > public object MyField
    > {
    > get
    > {
    > if (MyFieldType == FieldTypes.TypeA)
    > {
    > return (EnumA)m_MyField;
    > }
    > else
    > {
    > return (EnumB)m_MyField;
    > }
    > }
    > set
    > {
    > m_MyField = value;
    > }
    > }
    > }
    >
    >
    > My problem is that the MyField property is not editable
    > via a dropdown list in the designer. It appears as a
    > greyed-out (read only) string representation of the
    > respective enum (EnumA or EnumB). I've tried writing a
    > TypeConverter (inheriting from EnumConverter) but just
    > ended up going round in circles.
    >
    > Does anyone have an example of what I'm trying to do or
    > point me in the right direction?
    >
    > Thanks
     
    Teemu Keiski, Jan 18, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitch Mooney
    Replies:
    2
    Views:
    381
    Bob Hairgrove
    Jun 18, 2004
  2. Steve Knight
    Replies:
    2
    Views:
    780
    Steve Knight
    Oct 10, 2003
  3. Bit byte
    Replies:
    2
    Views:
    303
    Pavel
    Apr 16, 2006
  4. __PaTeR
    Replies:
    7
    Views:
    490
    Barry Schwarz
    Jan 1, 2009
  5. Replies:
    1
    Views:
    119
    Ken-Yi Lee
    Feb 2, 2014
Loading...

Share This Page