These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would like to suggest corrections or enhancements please drop me a note at Martin Schray
VB.NET - Enumerations Copyright Martin Schray
Enumerations Enumerations a simple way to strongly type – type information For example, the type of arguments for a messagebox or the types of loans available or any other argument type Good for when you a known list of aguments loans or messagebox icon types
Enumeration declaration Example Public Enum MessageTypes Informational Warning Emergency WorldEnding End Enum
Enum Use Example Public Class Form1 Inherits System.Windows.Forms.Form Public Enum MessageTypes Informational Warning Emergency WorldEnding End Enum Public Sub DisplayMessage(ByVal t As MessageTypes, ByVal s As String) If t.IsDefined(t.GetType, t) Then Debug.WriteLine(t) MessageBox.Show("Your message :" & s) Else ' display message, but probably should be an exception MessageBox.Show("Invalid Message Type :" & t) End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DisplayMessage(MessageTypes.Emergency, "world ending") DisplayMessage(7, "world ending") End Sub End Class
Enum comments Enum could be in a separate class Enum helped protected DisplayMessage from invalid arguments Enum used extensively in VB.NET (messagebox message types and such) When declared intellsense helps user fill in the right values