Main Page | Report this Page
.NET DotNet Forum Index  »  ASP.NET Forum  »  Entering an enumeration type as a property...
Page 1 of 1    

Entering an enumeration type as a property...

Author Message
Nathan Sokalski...
Posted: Wed Nov 04, 2009 2:15 pm
Guest
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
njsokalski at (no spam) hotmail.com
http://www.nathansokalski.com/
 
Captain Jack...
Posted: Wed Nov 04, 2009 2:29 pm
Guest
"Nathan Sokalski" <njsokalski at (no spam) hotmail.com> wrote in message
news:%23kdK0MYXKHA.3720 at (no spam) TK2MSFTNGP02.phx.gbl...
Quote:
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
njsokalski at (no spam) hotmail.com
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...
Posted: Wed Nov 04, 2009 5:38 pm
Guest
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
njsokalski at (no spam) hotmail.com
http://www.nathansokalski.com/

"Captain Jack" <CaptainJack1024 at (no spam) comcast.net> wrote in message
news:LJednS_qi_YjSGzXnZ2dnUVZ_gWdnZ2d at (no spam) giganews.com...
Quote:
"Nathan Sokalski" <njsokalski at (no spam) hotmail.com> wrote in message
news:%23kdK0MYXKHA.3720 at (no spam) TK2MSFTNGP02.phx.gbl...
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
njsokalski at (no spam) hotmail.com
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
 
Scott M....
Posted: Wed Nov 04, 2009 8:48 pm
Guest
"Nathan Sokalski" <njsokalski at (no spam) hotmail.com> wrote in message
news:upNq79ZXKHA.508 at (no spam) TK2MSFTNGP06.phx.gbl...
Quote:
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
njsokalski at (no spam) hotmail.com
http://www.nathansokalski.com/

"Captain Jack" <CaptainJack1024 at (no spam) comcast.net> wrote in message
news:LJednS_qi_YjSGzXnZ2dnUVZ_gWdnZ2d at (no spam) giganews.com...
"Nathan Sokalski" <njsokalski at (no spam) hotmail.com> wrote in message
news:%23kdK0MYXKHA.3720 at (no spam) TK2MSFTNGP02.phx.gbl...
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
njsokalski at (no spam) hotmail.com
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
 
Tom Shelton...
Posted: Wed Nov 04, 2009 9:16 pm
Guest
On 2009-11-04, Nathan Sokalski <njsokalski at (no spam) hotmail.com> wrote:
Quote:
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
 
Sergey Poberezovskiy...
Posted: Wed Nov 04, 2009 10:38 pm
Guest
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:

Quote:
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
njsokalski at (no spam) hotmail.com
http://www.nathansokalski.com/


.
 
Tom Shelton...
Posted: Wed Nov 04, 2009 11:21 pm
Guest
On 2009-11-05, Sergey Poberezovskiy <SergeyPoberezovskiy at (no spam) discussions.microsoft.com> wrote:
Quote:
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
 
Herfried K. Wagner [MVP]...
Posted: Sat Nov 07, 2009 6:56 pm
Guest
Nathan Sokalski schrieb:
Quote:
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/>
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Sat Dec 05, 2009 12:10 pm