complete-reference-vb_net_14
complete-reference-vb_net_14
Enum
This will print "Saturday" to the output window. Enum's ToString internally calls the Format method for
various output options, as described in Table 8−1. To return the ordinal value of DayEnum.Saturday, you
can specify the "D" format argument, which returns a Decimal value for Saturday. This is shown here:
Debug.WriteLine(dayoweek.ToString("D"))
You can also work directly with the Format method, which obviates the need to create an instance of the
type. Here's an example:
Console.WriteLine(DaysEnum.Friday.Format(E, 4, "G")
Table 8−2 provides an abridged explanation of the Format method's parameter− value options.
Console.WriteLine(DaysEnum.GetNames.Format(GetValues(E)))
If you just need to pass a value and recover its name, the GetName method will do the trick. Interestingly,
Microsoft did not implement an equivalent single value of the GetValues method. As you realize, returning
an array doesn't help much when you simply need to recover a single value for the symbol passed to the
method. An array also consumes more memory than a single value, especially if your type is an enumeration
of more than a handful of values.
I thought about implementing a method to capture the single value, but realized I could not derive from
System.Enum, so there is no way to override one of the above "Get" methods. However, we can make use of
the Parse method to solve our dilemma.
Parse lets us convert a symbol in an instance of Enum. We can then retrieve the value from the process that
represents the enumeration. This is shown in the following code:
Console.WriteLine([Enum].Parse(GetType(DepreciationPeriodEnum), "7"))
252