Visual Basic Operators |
|
Private Sub Form_Load() Dim NumberOfTracks As Integer NumberOfTracks = 16 End Sub
|
Practical Learning: Using Operators |
|
Unary Operators: The Negative Operator - |
As you can see on the above ruler, in order to express any number on the left side of 0, it must be appended with a sign, namely the - symbol. Examples are -12, -448, -32706. A value accompanied by - is referred to as negative. |
Practical Learning: Using The Negative Operator |
|
Arithmetic Operators |
The Addition + |
The addition is performed with the + sign. It is used to add one value to
another. The addition is also used to add the values of two variables. For example, you could use CDbl(txtMondayHours) + CDbl(txtTuesdayHours) to get a total number of hours worked on Monday and Tuesday. The result could be stored in another control. |
Practical Learning: Using the Addition Operator |
|
The Subtraction - |
The subtraction operation is performed using the - sign. This operation produces the difference of two or more numbers. It could also be used to display a number as a negative value. To subtract 28 from 65, you express this with 65-28. The subtraction can also be used to subtract the values of two values. |
Practical Learning: Using the Subtraction Operator |
|
The Multiplication * |
The multiplication operation allows you to add a number to itself a certain number of times set by another number. The multiplication operation is performed using the * sign. For example, to add 25 to itself 3 times, you would perform the operation as 25 * 3 |
Practical Learning: Using the Multiplication Operator |
|
The Integer Division: \ |
Dividing an item means cutting it in pieces or fractions of a set value. Therefore, the division is used to get the fraction of one number in terms of another. Microsoft Visual Basic provides two types of operations for the division. If you want the result of the operation to be a natural number, called an integer, use the backlash operator "\" as the divisor. The formula to use is: Value1 \ Value2 This operation can be performed on two types of valid numbers, with or without decimal parts. After the operation, the result would be a natural number.
|
Practical Learning: Using the Integer Division |
|
The Decimal Division: / |
The second type of division results in a decimal number. It is performed with the forward slash "/". Its formula is: Value1 / Value2 After the operation is performed, the result is a decimal number.
|
Practical Learning: Using the Decimal Division |
|
The Exponentiation: ^ |
Exponentiation is the ability to raise a number to the power of another number. This operation is performed using the ^ operator (Shift + 6). It uses the following formula: yx In Microsoft Visual Basic, this formula is written as: y^x and means the same thing. Either or both y and x can be values, variables, or expressions, but they must carry valid values that can be evaluated. When the operation is performed, the value of y is raised to the power of x. |
Practical Learning: Using the Exponent Operator |
|
The Remainder Operator: Mod |
The division operation gives a result of a number with or without decimal values, which is fine in some circumstances. Sometimes you will want to get the value remaining after a division renders a natural result. The remainder operation is performed with keyword Mod. Its formula is: Value1 Mod Value2 The result of the operation can be used as you see fit or you can display it in a control or be involved in another operation or expression. |
Visual Basic Operators |
Comments |
In the programming world, a comment is a piece of text in Visual Basic code that Visual Basic (in reality the compiler) would not consider when reading your code. As such a comment can be written any way you want. In Visual Basic, the line that contains a comment can start with a single quote. Here is an example: |
Private Sub Form_Load() ' This line will not be considered as part of the code End Sub |
Alternatively, you can start a comment with the Rem keyword. Anything on the right side of rem, Rem, or REM would not be read. Here is an example: |
Private Sub Form_Load() ' This line will not be considered as part of the code Rem I can write anything I want on this line End Sub |
Comments are very useful and you are strongly suggested to use comments regularly. They can never hurt your code and they don't increase the size of your application. Comments can help you and other people who read your code to figure out what a particular section of code is used for, which can be helpful when you re-visit your code after months or years of not seeing it. |
The Double Quotes: "" |
A string is an empty space, a character, or a group of characters that you type or provide to a control and you want this character or this group of characters to be considered "as is". In other words, the expression or the control that receives the string should keep it or them the way you supplied it or them unless you give other instructions regarding the string. A string can be an empty space or one character, such as $ or w; a group of characters, like home or Manchester United or Verbally speaking, I mean… Ah forget it. Most of the time, you will want the program to keep this character or group of characters exactly the way you or the user supplied them. In order to let the program know that this is a string, you must enclose it in double quotes. From our examples, our strings would be "$", "w", "home", "Manchester United", and "Verbally speaking, I mean… Ah forget it". To assign a string to an expression or a field, use the assignment operator as follows: Team = "Manchester United" In the same way, to initialize a variable with a string , use the assignment operator. Here is an example: |
Private Sub Form_Load() Dim Address As String Address = "12404 Lockwood Drive Apt D4" End Sub
The String Concatenator: & |
The & operator is used to append two strings, the contents of two controls, or expressions. This is considered as concatenating them. For example, it could allow you to concatenate a first name and a last name, producing a full name. The general syntax of the concatenation operator is: Value1 & Value2 To display a concatenated expression, use the assignment operator on the field. To assign a concatenated expression to a variable, use the assignment operator the same way: |
Private Sub Form_Load() Dim FirstName, LastName As String Dim FullName As String FirstName = "Francis " LastName = "Pottelson" FullName = FirstName & LastName Text0 = FullName End Sub
To concatenate more than two expressions, you can use as many & operators between any two strings or expressions as necessary. After concatenating the expressions or values, you can assign the result to another variable or expression using the assignment operator. |
The Parentheses Operators: () |
Parentheses are used in two main circumstances: in an event (or procedures, as we will learn) or in an operation. The parentheses in an operation help to create sections in an operation. This regularly occurs when more than one operators are used in an operation. Consider the following operation: 8 + 3 * 5 The result of this operation depends on whether you want to add 8 to 3 then multiply the result by 5 or you want to multiply 3 by 5 and then add the result to 8. Parentheses allow you to specify which operation should be performed first in a multi-operator operation. In our example, if you want to add 8 to 3 first and use the result to multiply it by 5, you would write (8 + 3) * 5. This would produce 55. On the other hand, if you want to multiply 3 by 5 first then add the result to 8, you would write 8 + (3 * 5). This would produce 23. As you can see, results are different when parentheses are used on an operation that involves various operators. This concept is based on a theory called operator precedence. This theory manages which operation would execute before which one; but parentheses allow you to completely control the sequence of these operations. |
The Line Continuation Operator: _ |
You will sometimes need to expand your code on more than two lines. This happens regularly if you are writing an expression that involves many entities that must belong to a group. To continue a piece of code from one line to the next, type an empty space followed by an underscore symbol, then continue your code on the next line. |
The Period Operator: . |
To access the property of an object, type the name of the object, followed by a period, followed by the name of the property you need. The property you are trying to use must be part of the properties of the object. If you know the name of the property, you can start typing it. Once the desired property is highlighted, press the Space bar or Tab. If you see the name of the property in the list, you can double-click click it. If the list doesn't appear, press Ctrl + Space bar. If you don't want to use the list displayed by the Code Editor, press Esc. Once you have specified what property you want to use, you can assign it the desired value or you can involve it in any operation you see fit. |
|
||
Previous | Copyright © 2001-2005 FunctionX, Inc. | Next |
|