Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - Entering an enumeration type as a property

 
Thread Tools Search this Thread
Old 11-04-2009, 07:15 PM   #1
Default Entering an enumeration type as a property


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
  Reply With Quote
Old 11-04-2009, 10:38 PM   #2
Nathan Sokalski
 
Posts: n/a
Default Re: Entering an enumeration type as a property
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
  Reply With Quote
Old 11-05-2009, 01:48 AM   #3
Scott M.
 
Posts: n/a
Default Re: Entering an enumeration type as a property

"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.
  Reply With Quote
Old 11-05-2009, 02:16 AM   #4
Tom Shelton
 
Posts: n/a
Default Re: Entering an enumeration type as a property
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
  Reply With Quote
Old 11-05-2009, 03:38 AM   #5
Sergey Poberezovskiy
 
Posts: n/a
Default RE: Entering an enumeration type as a property
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
  Reply With Quote
Old 11-05-2009, 04:21 AM   #6
Tom Shelton
 
Posts: n/a
Default Re: Entering an enumeration type as a property
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
  Reply With Quote
Old 11-07-2009, 11:56 PM   #7
Herfried K. Wagner [MVP]
 
Posts: n/a
Default Re: Entering an enumeration type as a property
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]
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
REVIEW: "Intellectual Property and Open Source", Van Lindberg Rob Slade, doting grandpa of Ryan and Trevor Computer Security 0 01-05-2009 06:15 PM
Stolen Macs / Laptops News Computer Support 4 05-11-2008 06:45 PM
Having isues installing 2007 pia junejune Computer Support 3 05-31-2007 05:43 AM
Rant: Intellectual Property, DRM, and Microsoft TechNews Computer Support 4 06-03-2004 07:49 PM
Anonymous Enumeration: a serious threat to Active Directory Eric Anderson Computer Security 0 11-08-2003 09:58 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46