![]() |
|
|
|||||||
![]() |
ASP Net - Entering an enumeration type as a property |
|
|
Thread Tools | Search this Thread |
|
|
#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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
"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. |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
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] |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |