Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Aug 14, 1998

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 i
Dim 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, c
DefInt i-k

Dim iCounter	' an integer
Dim 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)
Charles Caison
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date