Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
Chapter 5 Quick Reference
To | Do this |
Declare a variable | Type Dim followed by the variable name, the As keyword, and the variable data type in the program code. To make the variable valid in all of a form's event procedures, place this statement at the top of the code for the form, before any event procedures. For example:
Dim Country As String |
Change the value of a variable | Assign a new value with the assignment operator of (=). For example:
Country = "Japan" |
Get input with a dialog box | Use the InputBox function, and assign the result to a variable. For example:
UserName = InputBox("What is your name?") |
Display output in a dialog box | Use the MsgBox function. (The string to be displayed in the dialog box can be stored in a variable.) For example:
Forecast = "Rain, mainly on the plain." MsgBox(Forecast, , "Spain Weather Report") |
Create a constant | Type the Const keyword followed by the constant name, the assignment operator (=), the constant data type, and the fixed value. For example:
Const JackBennysAge As Short = 39 |
Create a formula | Link together numeric variables or values with one of the seven mathematical operators, and then assign the result to a variable or a property. For example:
Result = 1 ^ 2 * 3 \ 4 'this equals 0 |
Combine text strings | Use the string concatenation operator (&). For example:
Msg = "Hello" & "," & " world!" |
Include a class library from the .NET Framework | Place an Imports statement at the very top of the form's code that identifies the class library. For example:
Imports System.Math |
Make a call to a method from an included class library | Use the method name, and include any necessary arguments so that it can be used in a formula or a program statement. For example, to make a call to the Sqrt method in the System.Math class library:
Hypotenuse = Sqrt(x ^ 2 + y ^ 2) |
Control the evaluation order in a formula | Use parentheses in the formula. For example:
Result = 1 + 2 ^ 3 \ 4 'this equals 3 Result = (1 + 2) ^ ( 3 \ 4) 'this equals 1 |