Entering an enumeration type as a property

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

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.
 
N

Nathan Sokalski

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.
 
S

Scott M.

Nathan Sokalski said:
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.

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
 
T

Tom Shelton

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?
 
S

Sergey Poberezovskiy

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.
 
T

Tom Shelton

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.
 
H

Herfried K. Wagner [MVP]

Nathan said:
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'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top