0% found this document useful (0 votes)
6 views

complete-reference-vb_net_14

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

complete-reference-vb_net_14

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Working with System.

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.

Table 8−2: Options for the Format Method's Parameter Value

Format option Utility (abridged)


"G" or "g" If value, passed to the second argument, is equal to a named enumerated constant,
then the name of that constant is returned; otherwise, the decimal equivalent of
value is returned. (See also the discussion on flags later in this section.)
"D" or "d" The value is returned as a decimal value.
"X" or "x" The value is returned as a hexadecimal, sans the 0x notation.
"F" or "f" The value is treated as a bit field.
If you need to look at the Enum's collection of constants, you can retrieve an array of values using the
GetValues method. GetNames also returns an array, but instead of values you get the collection of names.
This code exemplifies both of the aforementioned methods:

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:

Dim periodVal As Integer


periodVal = [Enum].Parse(GetType(DepreciationPeriodEnum), "FifthYear")

and thus periodVal is initialized to 4. And in the following code

Console.WriteLine([Enum].Parse(GetType(DepreciationPeriodEnum), "7"))

the output to the console is EighthYear.

252

You might also like