Talk:Visual Basic/Introduction

From Wikiversity
Jump to navigation Jump to search

I just made a whole lot of new content for this page. Good luck sifting through it! :-D

Some places to link to or somehow incorporate knowledge from:

A place to chat with the people involved in this project would be nice so we're not stepping on eachothers' toes. I'm new to wikiversity, et al. I just figured out the basic structure of the courses, so I'm trying to work with that. I think some more brainstorming about course plan structure would be a good idea.

Removed content...[edit source]

I just removed the following content from this page. It's useful information, but highly inappropriate to an introductory article. Let's see if we can't find a better place for it. AmiDaniel (talk) 18:51, 1 March 2007 (UTC)[reply]

Object Types and Naming Scheme[edit source]

These are very important things to remember in order to make your source code more easily intelligible.

File Types[edit source]

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[edit source]

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[edit source]

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[edit source]

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 the hell 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[edit source]

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[edit source]

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[edit source]

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

General Overview of Coding[edit source]

Routine Types[edit source]

  • Sub: Returns nothing
    • Example:
' Description: Makes the window caption a random number between 1 and 100 every time the form is clicked

Option Explicit

Public intRandom As Integer

Private Sub Form_Click()
    Call GetNewRandom
    Me.Caption = intRandom
End Sub

Public Sub GetNewRandom()
    Randomize
    intRandom = Int(Rnd() * 100) + 1
End Sub

  • Function: Returns some sort of value
    • Example:
' Description: Makes a message box which displays the current military time at form load.
Option Explicit

Private Sub Form_Load()
    MsgBox "Current military time is: " & GetMilitaryTime
End Sub

Public Function GetMilitaryTime() As String
    GetMilitaryTime = Format$(Time, "hh:mm:ss")
End Function

Specific to user controls:

  • Let
  • Get
  • Set