Object Types and Naming Scheme

Level:
Level1

This tutorial explains all the different aspects of Visual Basic data types and constructs. It also gives suggestions as to how to name your different VB6 items. These are very important things to remember in order to make your source code more easily intelligible.

File Types

You will use various file types while using VB6. These are some the most common ones.

  • File type: What people call it
  • Prefix: What people prefix the filename with
  • Extension: What file extension is used
  • Description: What its used for most often
File Type Prefix Extension Description
Project   vbp General project options
Form frm frm GUI information and private code
BAS Module mod bas Project-wide accessible functions
Class Module cls cls Project-wide accessible subroutines
User Control uc ctl Control object (Like an OCX with source code)
Property page pag pag Property information
OLE Control   ocx Compiled control object
Dynamic Link Lib   dll Subs and functions accessible by other programs

Examples of common file names:

  • OddCalc.vbp
  • frmMain.frm
  • frmAbout.frm
  • frmPrintInvoice.frm
  • modMain.bas
  • modSettings.bas
  • modDeclares.bas
  • modWinsock.bas
  • clsWinsock.cls
  • ucCustomButton.ctl
  • ucTreeView.ctl
  • ucWinsock.ctl

Variables

A variable is a word or letter used to reference data used in a program. At the most basic level: All variables used in a program (Even if its interpreted as text) are held on the computer as a sequence of 1s and 0s (Binary) which represent numbers, which may or may not in turn represent letters or any given ASCII character.

In a sane programmer's code the variable names are easy to understand because they clearly state what the variable is used for inside of the variable name.

The information to be conveyed in a variable name is:

  1. Variable data type
  2. Functional use in program

This is accomplished by coming up with a unique word between about 3 and 10 letters which explains the functional use of the variable as well as a prefix of usually 3 letters which explains the variable data type.

A few examples of this:

  • intResult -- An integer which is the result of an operation
  • strFirstName -- A string which is used to store the first name of a person
  • dtmWorkDayEnd -- A Date variable which is used to store the time of the end of the work day

Computer Data Storage

All data stored on a computer is based upon binary values associated with them somewhere between 0 and 255. This is the range of values possible with an 8 bit binary value (8 ones and zeroes).

Decimal Binary Hexadecimal ASCII
48 0b00110000 0x30 0
49 0b00110001 0x31 1
50 0b00110010 0x32 2
------- --------------- --------- -----
55 0b00110111 0x37 7
56 0b00111000 0x38 8
57 0b00111001 0x39 9
------- --------------- --------- -----
65 0b01000001 0x41 A
66 0b01000010 0x42 B
67 0b01000011 0x43 C
------- --------------- --------- -----
88 0b01011000 0x58 X
89 0b01011001 0x59 Y
90 0b01011010 0x5A Z
------- --------------- --------- -----
97 0b01100001 0x61 a
98 0b01100010 0x62 b
99 0b01100011 0x63 c
------- --------------- --------- -----
120 0b01111000 0x78 x
121 0b01111001 0x79 y
122 0b01111010 0x7A z

A text character's "ASCII value" is the decimal value of the binary value used to represent that character on the computer. In the case of the uppercase letters A, the ASCII value is 65, which is 01000001 in binary. Uppercase Z has an ASCII value of 90, which is 1011010 in binary. The values for uppercase letters A through Z are between the values 65 and 90.

Anybody notice how similar the uppercase and lower case values are in binary and hex? In binary you toggle the 32's place in order to change case, and in hex you add/subtract 2 from the 16's place. Handy.

Associated example to play with:

Option Explicit
Private Sub Form_Load()
   Dim strChar As String ' Declares a variable
   
   ' Shows an input box and puts the result in a variable called strChar
   strChar = InputBox("What would you like the ASCII value of?", "HUH!? PUNK!?", "A")
   
   ' Shows a message box containing the ASCII value of the previously input letter
   ' plus a random ASCII uppercase letter
   MsgBox "The ASCII value of " & strChar & " is " & Asc(strChar) & vbNewLine & _
      "And your random, uppercase ASCII character is: " & _
      Chr$(RandomNumInRange(65, 90))
   ' Unloads the form (Which cleanly ends the program if no other forms are loaded)
   Unload Me
End Sub
Public Function RandomNumInRange(ByVal Low As Long, ByVal High As Long) As Long
   Randomize ' Randomizes Rnd() (Surprisingly good random number generator)
   
   ' Generates a random number between "High" and "Low" and returns it
   RandomNumInRange = Int((High - Low + 1) * Rnd) + Low
End Function

Variable Names

The following are the requirements when naming the variables in Visual Basic:

  • It must be less than 255 characters
  • No spacing is allowed
  • It must not begin with a number
  • Period is not permitted

For the sake of making sure other people can look at your code and know what you were thinking:

  • Suffix your variable name with the appropriate suffix for your variable's data type
  • Make sure the body of your variable name makes it easy to tell what its used for
  • Don't use an ambiguous name like "intUhhhh" or "strX" unless its use is within a very small scope of the program

Numeric Data Types

Type Size Range of Values Prefix Example Variable Name
Byte 1 byte 0 to 255 byt bytFirstChar
Integer 2 bytes -32,768 to 32,767 int intCount
Long 4 bytes -2,147,483,648 to 2,147,483,648 lng lngHwnd
Single 4 bytes Negative values: -3.402823E+38 to -1.401298E-45
Positive values: 1.401298E-45 to 3.402823E+38
sng sngPi
Double 8 bytes Negative values: -1.79769313486232e+308 to -4.94065645841247E-324
Positive values: 4.94065645841247E-324 to 1.79769313486232e+308
dbl dblAngle
Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807 cur curTotalCost

 

Non-numeric Data Types

Type Size Range of Values Prefix Example Variable Name
String(fixed length) Length of string 1 to 65,400 characters str strName
String(variable length) Length + 10 bytes 0 to 2 billion characters str strHTML
Date 8 bytes January 1, 100 to December 31, 9999 dtm dtmBirth
Boolean 2 bytes True or False bln blnToggle
Object 4 bytes Any embedded object obj objCurrent
Variant(numeric) 16 bytes Any value as large as Double vnt vntNumber
Variant(text) Length+22 bytes Same as variable-length string vnt vntName

Control Types 

Control Type Prefix
TextBox txt
PictureBox pic
Label lbl
Frame fra
CommandButton cmd
CheckBox chk
RadioButton rad
ComboBox cbo
ListBox lst
Scroll Bar sbr (no orientation needed)
Timer tmr
DriveListBox drv
DirListBox dir
FileListBox fil
Shape shp
Image img
Data dat
OLE ole
ListView lvw
TreeView tvw

Examples of common object names:

  • txtName
  • txtAddress
  • cboYear
  • cmdOK
  • cmdCancel

Hopefully this Visual Basic tutorial gave you good idea of all the different objects and data types in VB6. By following these naming standards and using the appropriate types in your code you will produce much better VB code.

This tutorail is released under the GNU Free Documentation License 1.2. The original can be found here.

 

If you enjoyed this post, subscribe for updates (it's free)

Thanks

I liked this site, I hope that I will get more about Vb6 in this site, Thanks to Vb6 Team

hahahahaha naka2tuwa amn

hahahahaha naka2tuwa amn yan!!!!!!!!!!!

peo masyadong mdame kci ehhh nkkbogsho

_ii LAB u PILT_
dHanica_o4 lHeidy kHuatroh

ang galing amn cguradong

ang galing amn cguradong madame aqung ma222nan d2

thank u xo much^_^

Yeah

This is the best!
I didnt read the whole tutorial but i think this will sure help me a lot
in developing useful programs in our company. Thanks! Keep posting useful ideas.

thanks

thanks very helpful tutorial
i like this site
many thanks

awesome site....

Have to admit this...great work boss!!! thanks for such a helpful site....u rock:-)

thanku

thanku

can i receive a daily tutorial of VB??

it's so nice to know that there's a site like this...
hope i can learn more in this kind of tutorial.
thank you!!

Why + 1?

Can anyone explain why the +1 is needed in the line

RandomNumInRange = Int((High - Low + 1) * Rnd) + Low

Why isn't it just:

RandomNumInRange = Int((High - Low) * Rnd) + Low

Another thing: There is no explanation of what range Rnd generates and why it's * Rnd. Agree with those who are puzzled. Unlike previous pages, this tutorial is not for complete newbies. A small amount of programming experience will help, and even then it hasn't helped me with the +1 query!

I Hope U'll Get It!

in case someone plays by passing high=low & ends up with the same no as random no.
it's clear that int((high-low)*Rnd)+low=low in this case

i think 0

I Hope U'll Get It!

in case someone plays by passing high=low & ends up with the same no as random no.
it's clear that int((high-low)*Rnd)+low=low in this case

Why + 1?

This function is supposed to give you integers as a result. Thus it used Int(x) to only use the digits left to the decimal point. If you just take Int, you will get results between 0 and (High-Low), because Rnd gives you a number in the intervall ]1; 0], i.e. between 0 (included) and 1 (NOT included)
****There is a problem though:*****
Though the statement is still faulty: it actually needs to write Int( (High - Low) *Rnd + Low + 0.5 ). You need to round correctly (not the VB round function, which is faulty) and thus use ( int(x+0.5))
This will give you the right result.

Quick explanetion of the rand formula
Rnd -> gives you a supposedly random number in [0;1[
High - Low -> scales the random number to match the width of the desired interval
+Low -> takes care of the offset, i.e. lower bound of your desired interval of randomness

By not using the Int(statement) you generate floating point numbers between [Low, High[
Introducing the Int(x+0.5) rounds these to nearest integer number.

I hope this helpes,
Blacky

I think it is to avoid the

I think it is to avoid the chance of getting the same result twice i.e. 0*1 and 1*0

PS

Forgot to say: thank you for this really nice site!

can i recieve daily tutorials.... of V.B 6

Im new user of VB.....
would you like to send me all tutorials??????
i want to make a small database application. and how can i store the data in that application....?
for example:
S.No
Name.... MOIN
F/Name M.Akbar
D.O.B ....Address.... contact no.....
plzzzz

Presumptuous...

I find this quite presumptuous in that it only mentions specifically one naming convention as there are actually several and neglects to mention even the standard convention names as though this is the only standard available. By only mentioning the one way, the WRITERS preferred way, this sets the reader up for difficulty and failure on several different levels. This also neglects to mention things like hungarian or reverse hungarian or any of the different aspects of different naming conventions. This convention closely follows the naming convention put forth by the VBE...
A quote from the book 'Hard Core VB' which conveys my feeling on this: "In reading working code and programming books over the years, I’ve seen a lot of styles, many of them well thought out and helpful to the development process. I’ve also seen bad variable names make some good code hard to read. Frequently, however, wretched naming conventions go hand in hand with wretched code. And one of the most wretched conventions is offered to you free by the Visual Basic environment."

I feel that this should be updated or replaced with something more viable to prevent any further difficulties for beginners.

Not Really

It's a VB6 site with VB6, presenting anything except the standard defined by the language's creator would be deviating into the "WRITERS preferred way" as you claim has happened. Quite the opposite.

This is THE standard for VB6. That in no way means it is the best convention, nor the only one you can use, but it is the VB6 standard none the less.

That said, what do you recommend? The VB naming convention can be confusing and unhelpful at times, so I'm always up for finding something better.

Abou the comments on this page -

Some one's go a lot of work to do with most of them !

still complicated.

i cant understand some terms.
you should've used baby terms so that newbies, like me, can easily cope up with this.

i have a question though,
can i use a variable like A?
for example: "Dim A as Integer" ? and if so, what's the difference if I use A as variable and strColor as my variable?

And one more thing..
what is the importance of " Public Function " and the " Modules "??

Programming variables

Where a programming language allows for long variable names using them will help you and later those maintaining the program to understand what is going on.

Ask yourself which is more understandable here:

a * x - d = t
or
lineprice * numberofitems - discount = totalprice

Would this be so bad?

Would this be so bad?:

PriceTotalDbl = PriceLineDbl * NumItemsLng - DiscountDbl

Or to seperate out the type suffixes:

PriceTotal_dbl = PriceLine_dbl * NumItems_lng - Discount_dbl

Or if you don't like the type suffixes at all:

PriceTotal = PriceLine * NumItems - Discount

By at least capitalizing each word [proper case] you can more easily read each item.

You may also note...

You may also note the reordering of the names so that like items show up together alphabetically which is especially useful with VB's intelisense popup lists [the standard vbe convention put forth in this article makes items of the same variable TYPE show together regardless of what it is for, which can make it very difficult to find what you are looking for.] 'PriceLine' and 'PriceTotal' will show up close to each other along with anything else which starts with "Price".

Should the newbie know this?

It's too hard to understand but I try to figure it out
I think that lesson should be add later

thanks

Conventions are very important

Naming conventions are very important to learn and use in order to keep your code consistant and useable. If you start with the conventions as you are just starting, you won't have to learn the conventions later. Since the website uses the same convention, it makes it easy to pick it up at the same time you are learning the fundamentals of programming in VB6.

For example, if you name all of your string variables from your first program on strVariable, you will be able to come back years later and look at that code and know exactly what kind of variable that is - it's a string variable.

There are other naming conventions you may find easier, but the most important things is that whatever you use, you are consistant with it. You should not be naming a string variable strVariable and an integer variable iVariable. Even worse is mixing conventions, like intVariable for one integer, iVariable for another, PurposeVariableInt for another, etc. You'll come back to it later and look at the code and go "Wtf is going on here?". Worse, you might be looking at somebody else's code and think "How am I supposed to figure this out?" It's not cool to leave inconsistant code for someone else to support.

decimal

I am trying to make a program which the result is in decimal form. what will i do? i enter a whole number of 100, i wanted it to become 100.00 as i leave the textbox. I multiplied it by 0.33 and i was expecting 33.00 as a result. please help me. it is my first time

can you have a program

can you have a program wherein the parameters of a 1-2 Shell and tube heat exchanger will be calculated?

Great Job really helped I

Great Job really helped I understood most of them perfectly from just a fair amount of computer knowledge.

hard

yeah i kinda found some bits confusing, ie what is the Option Explicit for?
aslo .. RandomNumInRange = Int((High - Low + 1) * Rnd) + Low '..... int + rnd????

hard

this was alittle hard for a newbie like me to understand couldnt u like Make it easier!

Nice work

nice tutorail, handy for checking the max values of things:)
cant belive some people dont understand it, its wonderful!