VB Constants

Variables which do not change their value during the execution of a program are called constants. Let us revisit the definition of a variable, which is, 'a variable has a name and a value and stores values during program execution'. So, we can say that constants are a special kind of variable, having a name and a value but do not change the value during program execution.

For instance, if your program uses the name of the constant in your vb code.

Public Const PURCHASE_CAT = 'a'


Suppose the value of PURCHASE_CAT (Purchase transaction) appears many times in a program. Instead of using the value 'a' over and over again, we can define a constant and name it as PURCHASE_CAT, and use the name of the constant in the program, as shown below.

Case InStr(1, PURCHASE_CAT + SALES_CAT + CREDIT_NOTE_CAT + DEBIT_NOTE_CAT, m_sCat) > 0 


Once a constant is declared, we can't change its value in subsequent statements. So, its value specified in the constant's declaration will remain same in the entire program. Note that, constants are processed faster than variables, because the compiler substitutes constant names with their values. We could declare 'PURCHASE_CAT' as a variable, but constants are preferred for two reasons:

1.Constants don't change value. This is a safety feature. Once a constant has been declared, we can't change its value in subsequent statements, so we can be sure that the value specified in the constant's declaration will take effect in the entire program.

2.Constants are processed faster than variables. When a program is running, values of constants don't have to be looked up. The compiler substitutes constant names with their values and the program executes faster.

The manner in which we declare constants is similar to the manner in which we declare variables, except that in addition to supplying the constant's name, we must also supply a value, as follows:

Const constant name As type = value

Public Const PURCHASE_CAT As String= "a"



Constants also have a scope and can be Public or Private. The constant PURCHASE_CAT, for instance, is usually declared in a module as Public so that every procedure can access it.

The name of the constant follows the same rules as variable names. The constant's value is a literal value or a simple expression composed of numeric or string constants and operators. We can't use functions in declaring constants.

Visual Basic uses constants extensively to define method arguments and control properties. The value of a CheckBox control, for instance, can be CheckState.Checked or CheckState.UnChecked. These constants correspond to integer values, but we don't need to know what these values are. We see only the names of the constants in the Properties window.

If we type the expression

CheckBoxl.CheckState =

a list of all possible values of the CheckState property will appear as soon as we type the equal sign, and we can select one from the list.

VB.NET recognizes numerous constants, which are grouped according to the property they apply to. Each property's possible values form an enumeration, and the editor knows which enumeration applies to each property as we type. As a result, we don't have to memorize any of the constant names or look up their names. They're right there as we type, and their names make them self-explanatory. Notice that the name of the constant is prefixed by the name of the enumeration it belongs to.

Enumerations are often named after the property they apply to, but not always. The set of possible values of the BorderSyle property for all controls is named BorderStyle enumeration. The value set for the alignment of the text on a control, however, is the Horizontal alignment enumeration. But we always see the proper enumeration in the Properties window, and the editor knows which one to display and when.

When developing an application, we can place all constants declarations in a single module. If we havemultiple modules which we use with several applications, try to include the module's name in the constant names to avoid conflicts and duplicate definitions.

In the title 'Database Programming using VB2005, C#, SQL Server 2005', we have shown how to place all the constants in a single module named constants.vb. This has facilitated placing of more than 20 constants pertaining to transaction type, group and account type in a single module, thus avoiding conflicts and duplicate definitions.






cover image of azure cloud book

Develop Azure Applications

Azure Cloud is gaining popularity and is competing with Amazon Elastic Cloud and Google Cloud for a spot in the billion dollar Cloud computing space.

With this book learn how to use - Table (an Azure Table is different from a SQL Server Table), Queue, Blob storage services, use your local machine to create an Application and a Service, how Azure splits a Table to achieve Load Balancing, Scalability based on the Partition Key, AppFabric Service Bus, Access Control Service, role of Fabric Controller, features of Azure SQL Database and Content Delivery Network.

Many have mastered Cloud Programming and are enjoying the benefits. Why not you?

View Highlights » View Contents »
cover image of foundation database programming book

Create your Own Accounting Software

A foundation book to Master Accounting Software Development, VB.Net, C# and OOPs programming. Detailed explanation of the Accounting domain for programmers is included. SQL and ADO.Net is also covered in detail.

This book gives you a strong foundation in Application Development.

A thorough understanding of the contents will enable you to grasp the topics covered in the accompanying books quite easily. This is because, the accounting domain is dealt with great detail in this book.

The ideal book to get started with application development in the .Net world.

View Highlights » View Contents »

cover image of asp.net book

Create your Own WEB ERP Software using ASP.Net

This book opens you to the Web. Using ASP.Net, C# and SQL Server as the tools, you will learn to build a Multi Tier Web Application as recommended by Microsoft. You will learn implementing inline GridView control, Transaction Management for a layered Web Application, build Data Access component and a Business Layer component.

Opens you to a world of opportunities. You will be able to develop a Web ERP application including all the five modules and learn how to use the TreeView control and GridView control to implement transactions such as Purchase Invoice, Sales Order and Sales Invoice.

View Highlights » View Contents »
cover image of entity framework book

ASP.Net MVC 5 Application Development Using Entity Framework 6 and VS 2015

Entity frameworks is the latest buzz in .Net programming and is the data access technology for Microsoft OS based hand held devices and web applications. Learn to integrate EF into an ASP.Net Application and MVC application. Create, explore and query the Entity Data Model. Learn how to use ObjectDataSource control and GridView control in an ASP.Net Application.

Also, learn to build Three-Tier ASP.Net application, create a business-class, implement methods in the class to perform CRUD operations and bind a GridView to the ObjectDataSource control. Learn how to handle concurrency and managing Transactions.

View Contents »