Talk:Visual Basic/Introduction

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:
 * http://hashvb.earlsoft.co.uk/Main_Page
 * Anything on this list: http://en.wikiversity.org/wiki/Special:Search?search=vb6&go=Go
 * http://en.wikiversity.org/wiki/API_use_with_VB6
 * http://en.wikiversity.org/wiki/Variables_and_Types_in_VB6
 * http://en.wikiversity.org/wiki/Functions_and_Subroutines_in_VB6

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... =

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)

Object Types and Naming Scheme
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

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).

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 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

Control Types
Examples of common object names:
 * txtName
 * txtAddress
 * cboYear
 * cmdOK
 * cmdCancel

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

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

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

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