Enums for Everyone
In this issue:
Let's continue our exploration into the world of enums. This is a special type of .NET class that contains a static list of values. This is often used when you need to reference a known, or pre-defined set of values. In the first article I showed you how enums are defined and used. Today, I want to discuss how you can use them in your projects.
Properties vs Enums
But first, I want to make sure you understand a subtle distinction. You might reference something in .NET like this:
PS C:\> [System.Drawing.Color]::Violet
R : 238
G : 130
B : 238
A : 255
IsKnownColor : True
IsEmpty : False
IsNamedColor : True
IsSystemColor : False
Name : Violet
It is easy to make the leap and think [System.Drawing.Color] is an enum. It sure feels like one. However, the class tells us otherwise.
PS C:\> [System.Drawing.color]
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Color System.ValueType
PS C:\> [System.Drawing.color].isEnum
False
Using Get-TypeMember show that Violet is technically a property not an enumeration.
PS C:\> Get-TypeMember System.Drawing.Color -Name Violet
Type: System.Drawing.Color
Name MemberType ResultType IsStatic IsEnum
---- ---------- ---------- -------- ------
Violet Property Color
Compare that to this class:
PS C:\> [System.Drawing.FontStyle]::Bold
Bold
PS C:\> [System.Drawing.FontStyle]
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True FontStyle System.Enum
PS C:\> [System.Drawing.FontStyle].IsEnum
True
[System.Drawing.FontStyle] is an enum. Even though you reference values and properties the same way, using the :: operator, the classes are structured differently. You need to be able it identify an enum class because it might affect how you use the class in your code.
Create Your Own
Moving on, let's create a custom enum. You can easily create on right from a PowerShell prompt using the enum keyword.
enum fruit { apple; banana; blueberry; cherry; grape; kiwi; lemon; lime }
> I recommend keeping the enum name simple, yet concise. Don't include spaces or symbols in the enum name.
I can now this enum for the duration of my PowerShell session.
PS C:\> [fruit]::apple
apple
PS C:\> [fruit]
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True fruit System.Enum
PS C:\> Get-TypeMember fruit
Type: fruit
Name MemberType ResultType IsStatic IsEnum
---- ---------- ---------- -------- ------
apple Field fruit True
banana Field fruit True
blueberry Field fruit True
cherry Field fruit True
grape Field fruit True
kiwi Field fruit True
lemon Field fruit True
lime Field fruit True
GetType Method Type
HasFlag Method Boolean
ToString Method String
At the console, I defined the enum with a one-line bit of code. In a script file, the typical way to define an enum is like this:
enum fruit {
apple
banana
blueberry
cherry
grape
kiwi
lemon
lime
}
It is much easier to read in this form. The values must be static. You can't use variables or code to define an item in the enum. Also remember that there is no way to dynamically add or remove items from the enum once it has been defined in your PowerShell session. Although you can always redefine it with a enum command.