Entering an enumeration type as a property

Discussion in 'ASP .Net' started by Nathan Sokalski, Nov 4, 2009.

  1. I have an ASP.NET Control that takes an enumeration type as a property. If I
    hardcode the type into my code as follows:

    Dim names() As String = System.Enum.GetNames(GetType(NathanSokalski.Months))

    Everythig works fine, but if I use the property, as in the following:

    Dim names() As String = System.Enum.GetNames(Me._enumerationtype)

    I recieve the following error:

    Cannot create an object of type 'System.Type' from its string representation
    'NathanSokalski.Months' for the 'EnumerationType' property.

    How can I allow the user to enter an enumeration type as a property? Any
    help (or examples) would be appreciated. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Nov 4, 2009
    #1
    1. Advertising

  2. I tried that, and it gives me the same error. I don't know if it is the
    problem or not, but here is the declaration of the property that the user
    uses to enter the enumeration type:

    Private _enumerationtype As System.Type

    Public Property EnumerationType() As System.Type
    Get
    Return Me._enumerationtype
    End Get
    Set(ByVal value As System.Type)
    Me._enumerationtype = value
    End Set
    End Property

    Any other ideas? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Captain Jack" <> wrote in message
    news:...
    > "Nathan Sokalski" <> wrote in message
    > news:%...
    >>I have an ASP.NET Control that takes an enumeration type as a property. If
    >>I hardcode the type into my code as follows:
    >>
    >> Dim names() As String =
    >> System.Enum.GetNames(GetType(NathanSokalski.Months))
    >>
    >> Everythig works fine, but if I use the property, as in the following:
    >>
    >> Dim names() As String = System.Enum.GetNames(Me._enumerationtype)
    >>
    >> I recieve the following error:
    >>
    >> Cannot create an object of type 'System.Type' from its string
    >> representation 'NathanSokalski.Months' for the 'EnumerationType'
    >> property.
    >>
    >> How can I allow the user to enter an enumeration type as a property? Any
    >> help (or examples) would be appreciated. Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    > Adding a call to the GetType() method of the property might do the trick,
    > as in:
    >
    > Dim names() As String =
    > System.Enum.GetNames(Me._enumerationtype.GetType())
    >
    > --
    > Jack
    >
     
    Nathan Sokalski, Nov 4, 2009
    #2
    1. Advertising

  3. Nathan Sokalski

    Scott M. Guest

    "Nathan Sokalski" <> wrote in message
    news:...
    >I tried that, and it gives me the same error. I don't know if it is the
    >problem or not, but here is the declaration of the property that the user
    >uses to enter the enumeration type:
    >
    > Private _enumerationtype As System.Type
    >
    > Public Property EnumerationType() As System.Type
    > Get
    > Return Me._enumerationtype
    > End Get
    > Set(ByVal value As System.Type)
    > Me._enumerationtype = value
    > End Set
    > End Property
    >
    > Any other ideas? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    > "Captain Jack" <> wrote in message
    > news:...
    >> "Nathan Sokalski" <> wrote in message
    >> news:%...
    >>>I have an ASP.NET Control that takes an enumeration type as a property.
    >>>If I hardcode the type into my code as follows:
    >>>
    >>> Dim names() As String =
    >>> System.Enum.GetNames(GetType(NathanSokalski.Months))
    >>>
    >>> Everythig works fine, but if I use the property, as in the following:
    >>>
    >>> Dim names() As String = System.Enum.GetNames(Me._enumerationtype)
    >>>
    >>> I recieve the following error:
    >>>
    >>> Cannot create an object of type 'System.Type' from its string
    >>> representation 'NathanSokalski.Months' for the 'EnumerationType'
    >>> property.
    >>>
    >>> How can I allow the user to enter an enumeration type as a property? Any
    >>> help (or examples) would be appreciated. Thanks.
    >>> --
    >>> Nathan Sokalski
    >>>
    >>> http://www.nathansokalski.com/
    >>>

    >>
    >> Adding a call to the GetType() method of the property might do the trick,
    >> as in:
    >>
    >> Dim names() As String =
    >> System.Enum.GetNames(Me._enumerationtype.GetType())
    >>
    >> --
    >> Jack
    >>


    How are you setting the EnumerationType() property? It appears as if you
    are setting it to a String, which is a type and therefore is allowed, but
    when you go to actually get the enum members, the CLR can't understand that
    the String you entered is really representative of an Enum. This is why,
    even when you use GetType() on the end, the problem still persists, all you
    are getting is a String type back, not the actual enum type.

    If you ensure that the property, EnumerationType(), is only set with a valid
    Enum (and not the String name of the Enum), you should be ok.

    -Scott
     
    Scott M., Nov 5, 2009
    #3
  4. Nathan Sokalski

    Tom Shelton Guest

    On 2009-11-04, Nathan Sokalski <> wrote:
    > I tried that, and it gives me the same error. I don't know if it is the
    > problem or not, but here is the declaration of the property that the user
    > uses to enter the enumeration type:
    >
    > Private _enumerationtype As System.Type
    >
    > Public Property EnumerationType() As System.Type
    > Get
    > Return Me._enumerationtype
    > End Get
    > Set(ByVal value As System.Type)
    > Me._enumerationtype = value
    > End Set
    > End Property
    >
    > Any other ideas? Thanks.



    You might be looking for System.Type.GetType(string) - or a similar overload.

    How are you setting the EnumerationType property in the first place?

    --
    Tom Shelton
     
    Tom Shelton, Nov 5, 2009
    #4
  5. Nathan,

    It looks like your _enumerationType variable is a string, as otherwise your
    code should work just fine. Make sure that it is a System.Type.

    Otherwise you may need to post some more code to show this property.

    "Nathan Sokalski" wrote:

    > I have an ASP.NET Control that takes an enumeration type as a property. If I
    > hardcode the type into my code as follows:
    >
    > Dim names() As String = System.Enum.GetNames(GetType(NathanSokalski.Months))
    >
    > Everythig works fine, but if I use the property, as in the following:
    >
    > Dim names() As String = System.Enum.GetNames(Me._enumerationtype)
    >
    > I recieve the following error:
    >
    > Cannot create an object of type 'System.Type' from its string representation
    > 'NathanSokalski.Months' for the 'EnumerationType' property.
    >
    > How can I allow the user to enter an enumeration type as a property? Any
    > help (or examples) would be appreciated. Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    >
    > .
    >
     
    Sergey Poberezovskiy, Nov 5, 2009
    #5
  6. Nathan Sokalski

    Tom Shelton Guest

    On 2009-11-05, Sergey Poberezovskiy <> wrote:
    > Nathan,
    >
    > It looks like your _enumerationType variable is a string, as otherwise your
    > code should work just fine. Make sure that it is a System.Type.
    >
    > Otherwise you may need to post some more code to show this property.
    >
    > "Nathan Sokalski" wrote:


    What it looks like is he is trying to assign a type from a string name in a
    variable. System.Type.GetType(string) or related over load is what he is
    probably looking for.

    --
    Tom Shelton
     
    Tom Shelton, Nov 5, 2009
    #6
  7. Nathan Sokalski schrieb:
    > I tried that, and it gives me the same error. I don't know if it is the
    > problem or not, but here is the declaration of the property that the user
    > uses to enter the enumeration type:
    >
    > Private _enumerationtype As System.Type
    >
    > Public Property EnumerationType() As System.Type
    > Get
    > Return Me._enumerationtype
    > End Get
    > Set(ByVal value As System.Type)
    > Me._enumerationtype = value
    > End Set
    > End Property


    I wonder how the user is entering the type here. Maybe you are trying
    to assign a string containing the type name to this property and the
    property access is "protected" by a 'Try...Catch' block?

    According to the property definition you can only assign a 'Type'
    object. In order to get a 'Type' object based on the string
    representation of its name, you may want to use 'Type.GetType'.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
     
    Herfried K. Wagner [MVP], Nov 7, 2009
    #7
    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. Earl Teigrob
    Replies:
    4
    Views:
    522
    Earl Teigrob
    May 19, 2004
  2. japh
    Replies:
    4
    Views:
    784
  3. Jeff
    Replies:
    3
    Views:
    582
    Karsten Baumgarten
    Feb 5, 2005
  4. puvit82
    Replies:
    4
    Views:
    781
    puvit82
    Feb 1, 2008
  5. Jon

    Databinding to a collection with a enumeration property

    Jon, Jul 13, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    132
    Eliyahu Goldin
    Jul 14, 2004
Loading...

Share This Page