Did you ever got frustrated about the limitations of Enums? Ever needed an Enum that supports Strings? Or your custom type?
Did you ever wonder why you can't add any instance methods and properties to an Enum?
Do you like to be able to enumerate over all members, ask their names, values and index? Or just call one of your custom method on the instances (strongly typed and real instance methods, not extenstion methods)?
Did you ever wonder why you need to specify a value when the enum itself would satisfy your needs? Got annoyed to always have to include that "None"-member?
If you answer some of this questions with yes, CustomEnum.vb is definitely something for you, except, of course, if you are a religious C# believer then you need to wait a little bit for the C# version. I have the intension to provide it somewhen soon but I have the feeling it is not done by simply pushing it through the converter.
Although this is version 1.0, CustomEnum.vb is a carefully written, highly optimized, with a high code coverage tested and well documented source file written in VB.NET that provides 2 base classes called "CustomEnum" and "ValueEnum" with generic type parameters that allow you to build your own Enums according your individual needs.
The first generic type parameter is always the type of your Enum (the subclass), the optional last one the type of the combination (Enum.Combi), and the second one (ValueEnum only) is the type of the value.
Here an example:
Public Class ShirtSize
Inherits CustomEnum(Of ShirtSize)
Private Sub New()
Public Shared ReadOnly S As New ShirtSize()
Public Shared ReadOnly M As New ShirtSize()
Public Shared ReadOnly L As New ShirtSize()
Public Shared ReadOnly XL As New ShirtSize()
The usage is the same as with standard Enums:
Dim myShirt As Object = GetShirt(ShirtSize.XL)
Private Function GetShirt(size As ShirtSize) As Object
Note that Enums are now reference types and may be null. Although this forces you to always check for null you never have to include that "None" member in your Enum anymore...
If you like to work with "Select Case" that's still possible, you just have to append the ".Index" to the member, eg. ShirtSize.XL.Index.
But CustomEnum also provides equals/inequals operators, you can use them like this "If (size = ShirtSize.XL) Then ..."
Did I already mention that CustomEnum fully supports member combinations like with the FlagsAttribute? They are implemented as an inner class called "Combi" and there are functions and operators to construct them:
SetHighSeasons(Season.Spring Or Season.Summer Or Season.Autumn)
And the method looks like this:
Private Sub SetHighSeasons(seasons As Season.Combi)
For Each mySeason As Season In seasons
There is many more to it:
- CustomEnum is thread-safe.
- CustomEnum checks and enforces correct implementation.
- CustomEnum has lots of attributes applied to simplify editing, debugging and code-analysis.
- CustomEnum allows to have subclasses for common functionality (although you cannot inherit members).
- CustomEnum runs in partially trusted code as well as in full trust
- CustomEnum does not make use of LINQ nor extension methods and is pure .NET 2.0.
- CustomEnum has no dependencies to other packages.
- CustomEnum only references mscorlib.dll and System.dll.
- CustomEnum ships as one single source file (and an additional tutorial folder that can easily be deleted).
- CustomEnum is free of charge.
Thank you for your interest in this package, feed-back is welcome, our email naming convention is
Install-Package CustomEnum.vb -Version 1.0.4
dotnet add package CustomEnum.vb --version 1.0.4
<PackageReference Include="CustomEnum.vb" Version="1.0.4" />
paket add CustomEnum.vb --version 1.0.4
Changes in 1.0.4:
Correction of typos in documentation and code-analysis-suppression-comments, additional tutorial page added with autoincremented values.
- No dependencies.
This package is not used by any popular GitHub repositories.