Simplify Variable Declarations by Using Default Types

Simplify Variable Declarations by Using Default Types

Hungarian notation and similar naming schemes tell you at a glance the data type of any variable in your code. You know that the variable intCounter is an integer, and you know that strName is a string.
You can simplify your variable declarations by telling VB that any variable you declare that begins with a certain letter will always be a certain type. This relieves you of having to include the AS DataType clause in your variable declarations, but you can still take advantage of the benefits of explicit variable type declaration. This code informs VB that all variables beginning with the letter “i” will be integers:

 DefInt iDim iCounter

If you use the Visual Basic function TypeName() and pass it the variable iCounter, it returns “Integer” rather than “Variant.” In addition to specifying a specific character, you can also specify a range or list of letters. This code assigns variables that begin with the letters i, j, or k as integers, and those variables that begin with s or c as strings:

 DefStr s, cDefInt i-kDim iCounter	' an integerDim sName	' a string

Here are more default variable type declarations:

 DefBool (Boolean)DefByte (Byte)DefLng (Long)DefCur (Currency)DefSng (Single)DefDbl (Double)DefDec (Decimal)DefDate (Date)DefStr (String)DefObj (Object)DefVar (Variant)
See also  Monetize TikTok For Your Business

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist