| Basic Concepts | |
| Working with Variables | |
| Like all
programming languages, VB has variables that store the data used in the
programs. There are many types of variables in VB, such as: Integer - for whole numbers between -2,100,000 and 2,000,000 Double- for numbers with decimal places ( 3.128325) String - for text and characters Boolean - for true and false (binary) data To use variables, it is also important to understand the concept of scope. Scope basically is about where the variable belongs. When you create a variable inside a subroutine (or function) it is considered to have local scope. This meaning the variable only can be effected by things within that subroutine itself, and not things from the outside, like other subroutines. You can create more global scope by defining a variable outside of all the subroutines, and within the form itself. These kinds of variables can be altered by anything within that form, any function or subroutine has access to them. So how do we define a variable in VB, you do so as the following syntax: Dim VariableName as VariableTypeExamples: Dim Age as Integer Dim Alive as Boolean Dim Pi as Double Dim PlayerName as String Dim People (1 to 10) as Integer (this example shows an array) Dim Height as Integer, Weight as Integer How to manipulate Variables You can do many of things with variables obviously. If you want to set up a variable with a value from the start all you have to do is something likeAge = 15 CompanyName = "The Crap Factory" Want to add values together? Weight = 150 ChineseFood = 20 Weight = Weight + ChineseFood (Weight now equals 170) First = "Homer", Last = "Simpson" Name = First + Last (Name now equals "HomerSimpson") Name = First + " " + Last (Name now equals "Homer Simpson") |
|
| Conditional Statements | |
| = | Equal to |
| <> | Not Equal to |
| > | Greater Than |
| < | Less Than |
| >= | Greater or Equal to |
| <= | Lesser or Equal to |
| If Statements | |
| If Age
>= 18 Then PersonCanVote = True Else PersonCanVote = False End If If Value = 1 Then MsgBox "The value is 1" ElseIf Value = 2 Then MsgBox "The value is 2" Else If Value = 10 then MsgBox "The Value is 10" End If MsgBox "The value is something else" End If |
|
| For Loop | |
| For i = 0
to 10 Value = Value + 1 Next i This adds 1 to Value every time until it hits the number 10 |
|
| Do ... Until Loop | |
| Value = 0 Do Value = Value + 1 Loop Until Value > 10 This adds 1 to Value every time until it hits the number 10 |
|
| Select Case | |
| Select
Case Value Case 0: Msgbox "Hello" Case 1: Case 2: Case 3: MsgBox "I am a Taco" End Select This makes a message box appear and say "Hello" if the value of Value is 0, if Value = 1,2 or 3 then it will make a message box that will say "I am a Taco". |
|