|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Cast from string to System.DayOfWeekI am trying to cast a string to System.DayOfWeek. Don't even know if
this is possible but it would sure save me a lot of headache if it is. Trying... Dim strDay As String = "Wednesday" Dim day As System.DayOfWeek = ctype(strDay, System.DayOfWeek) Charlie Brown wrote:
> I am trying to cast a string to System.DayOfWeek. Don't even know if DayOfWeek is an Enum, and Enum has the Parse method. The syntax is a> this is possible but it would sure save me a lot of headache if it is. > > Trying... > > Dim strDay As String = "Wednesday" > Dim day As System.DayOfWeek = ctype(strDay, System.DayOfWeek) little ugly, but it works: day = CType(System.Enum.Parse(GetType(DayOfWeek), strDay), DayOfWeek) (The reason we have to say System.Enum is because Enum is a VB.NET keyword. An alternative syntax is day = CType([Enum].Parse(GetType(DayOfWeek), strDay), DayOfWeek) which isn't really much better). You will get an ArgumentException if the string doesn't match any of the enum values. -- Larry Lard Replies to group please Larry Lard wrote:
Show quoteHide quote > Charlie Brown wrote: Oops, with a pre-defined enum we can be a little cleaner:> > I am trying to cast a string to System.DayOfWeek. Don't even know if > > this is possible but it would sure save me a lot of headache if it is. > > > > Trying... > > > > Dim strDay As String = "Wednesday" > > Dim day As System.DayOfWeek = ctype(strDay, System.DayOfWeek) > > DayOfWeek is an Enum, and Enum has the Parse method. The syntax is a > little ugly, but it works: > > day = CType(System.Enum.Parse(GetType(DayOfWeek), strDay), > DayOfWeek) day = CType(DayOfWeek.Parse(GetType(DayOfWeek), strDay), DayOfWeek) using the fact that Parse can be invoked directly on a type that inherits from Enum. -- Larry Lard Replies to group please
At a loss figuring out if an IP is on LAN or INET
update form background color Function call problem DWORDS ? LONG? - I need to create constants for the following How to Obsolete two related classes? Calling PaintBox Paint event Invalid Cast Errors Can't open a project by iteself? Solution opens every time! VS.NET 2005 Invoking apps within VB |
|||||||||||||||||||||||