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
|
|
|
|
|
|
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- The True Costs of Virtual Server Solutions VMware Discover ways to streamline and simplify your assessment of the total acquisition costs of a server virtualization environment. Download Now
- Email Security and Archiving - Clearer in the Cloud Google The time is NOW for businesses and organizations of all sizes to implement ... Download Now
- Why Isn't Server Virtualization Saving Us More? A Few Small Changes May Dramatically Increase Your Efficiency VMware Ever wonder why your company isn't saving more from its server virtualization? Making a few small changes could dramatically increase your efficiency. Download Now
- Building the Virtualized Enterprise with VMware Infrastructure VMware This paper explains how adopting a virtual infrastructure -- comprised of server, storage, and networking virtualization technologies -- can help your organization build a sustainable competitive ... Download Now
- The Impact of Virtualization Software on Operating Environments VMware Today's use of virtualization technology allows IT professionals to ... Download Now
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





