Bundle related elements in VB.NET with enumeration
Takeaway: In general, enumerations assist in bundling together related elements in Visual Basic .NET. Here's a tip that shows how enumeration makes code easier to develop and read.
You'll find that using enumeration in your VB.NET code comes in handy whenever you need to establish a set of related constants in your application and set their values. In this quick tip, I give an example of how using enumeration makes code easier to develop and read.
Enumeration at a glance
After you define your enumeration, you can refer to its individual members simply by combining the enumeration name with the member name. Once enumeration is defined, Visual Studio provides the IntelliSense feature where, when you start typing, it recognizes the possible members and allows you to select them from the list without necessarily typing the full member name. View Figure 1.
Example
Add the following code to the top of your module right after the class declaration statement:
Enum Environment As Integer
Production = 100
COB = 200
QA = 300
Development = 400
End Enum
Then add the following code to the Form_Load event:
Dim iEnvironment As Integer
iEnvironment = Environment.Production
MessageBox.Show("Production = " & iEnvironment)
iEnvironment = Environment.Development
MessageBox.Show("Development = " & iEnvironment)
In the example, I define a set of related variables that signify certain given values that would normally be required for the application at the top of the class. Then, I use the values in the code by simply defining an integer variable (i) and setting its value to a particular member of the enumeration (Prod). Then, I display the resulting value (100) in the MessageBox. I then reassign a different value to it (Development) and display the new value in the MessageBox (400).
The result of running the code would look like Figures 2 and 3.
Miss a tip?
Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!
Print/View all Posts Comments on this article
SponsoredWhite Papers, Webcasts, and Downloads
- The Economist: A new mandate for IT SAP
- CRM Without Compromise: A Strategy for Profitable Growth SAP
- Now is the Time for HD Video Communications - The Smart Business Tool GBH Communications
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET






