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)


Share the Post: