You can use the value locally on the visitor's computer, or you can let the web page send it to the server. In both cases, the value would have to be stored in the computer memory. Your first job is to identify and store that value. Based on this functionality, the value is called a variable. A variable is a value temporarily stored in the computer memory so the value can easily be identified, called, accessed, and used when necessary.
Temporarily storing a value in the computer memory is referred to as declaring a variable. To declare a variable, you start with the Dim keyword: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim %> </body> </html>
A variable must have a name, which is also referred to as an identifier. There are rules you must follow when naming your variables. The rules to follow are:
A name cannot be one of the following reserved words: Although you must always avoid using keywords as names of your variables in your program, if you insist on using one of these keywords to name something, put the word between square brackets. An example would be [True] The Visual Basic language is not case sensitive. This means that NAME, name, and Name represent the same word. This means that, in the same section (normally called scope), you cannot have two variables with the same name that differ only by their cases. This would cause a name conflict. You should always make sure that you declare a variable before using it. Otherwise you may use two variables that seem to be different but because of a mistype, you would think that you are using two variables. Examples are Type and Tape. To indicate that each variable must be declared prior to being used, in the top section of your source file, you should type: Option Explicit On
Besides a name, when declaring variable, you must specify the amount of memory space that the variable would need. The amount of memory is referred to as a data type or a type.
You can declare a variable but not specify the type of value that would be stored in the memory area reserved for it. When you have declared a variable, it receives an initial value. You can provide an initial value to a newly declared variable. To initialize a variable, type its name, followed by =, and followed by the desired value. Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim CustomerName CustomerName = "Paul Bertrand Yamaguchi" %> </body> </html> After initializing the variable, the new value is stored in its reserved area of memory. After declaring and initializing a variable, you can change its value whenever you judge it necessary. To change the value of a variable, assign a new value to it. Here are examples:<%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim CustomerName CustomerName = "Paul Bertrand Yamaguchi" %> <% CustomerName = "Raymond Kouma" %> </body> </html> You can change and re-change the value of a variable as many times as you want.
In Lesson 2, we saw that you could use an input box to request a value from a visitor. We saw that, to do this, you could type a sentence in the parentheses of InputBox(). Here is an example: InputBox("Enter your first name: ")
In Lesson 3, we saw that you could use Response.Write() to display a value on a web page. We saw that you could enter a sentence in the parentheses. Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim CustomerName CustomerName = "Paul Bertrand Yamaguchi" %> <% Response.Write("Customer Name: ") %> </body> </html> You can also type the name of a variable in the parentheses of Response.Write(). Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim CustomerName CustomerName = "Paul Bertrand Yamaguchi" %> <% Response.Write("Customer Name: ") Response.Write(CustomerName) %> </body> </html>
An operation is an action performed on one or more values either to modify one value or to produce a new value by combining existing values. Therefore, an operation is performed using at least one symbol and one value. The symbol used in an operation is called an operator. A variable or a value involved in an operation is called an operand. A unary operator is an operator that performs its operation on only one operand. An operator is referred to as binary if it operates on two operands.
If you want to write a long line of code, you can spread it on various lines. To do it, at the end of the line of code, put an empty space, then type the underscore, and continue your code in the next line.
There are various ways parentheses are used. In an operation, parentheses help to create sections in an operation. This regularly occurs when more than one operators are used in an operation.
The comma is used to separate variables used in a group. For example, a comma can be used to delimit the names of variables that are declared on the same line. Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim CustomerName, AccountNumber, InitialDeposit Dim RegularDeposit, TotalDeposits </body> </html> The comma can also be used in many other circumstances we will review in future lessons. The assignment operation is used to make a copy of a value or the value of a variable and give the copy to another variable. The assignment operation is performed with the = sign. After you have declared a variable, before using it, it must have a value. One way you can give a value to a variable is to assign one.
A double-quote is used to delimit a group of characters and symbols. To specify this delimitation, the double-quote is always used in combination with another double-quote, as in "". What ever is inside the double-quotes is the thing that need to be delimited. The value inside the double-quotes is called a string. Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim Country Country = "Emirats Arabes Unis" </body> </html>
By default, each Visual Basic statement is supposed to be on its own line. Here are examples: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim FirstName Dim LastName FirstName = "Charles" LastName = "Nanze" %> </body> </html> Still, you can write various statements on the same line. To do this, the statements can be separated by a colon. Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim FirstName Dim LastName FirstName = "Charles" : LastName = "Nanze" %> </body> </html>
The & operator is used to append two strings 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: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim FirstName Dim LastName Dim FullName FirstName = "Francis " LastName = "Pottelson" FullName = FirstName & LastName %> </body> </html> 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 addition is performed with the + sign. It is used to add one value to another. Besides arithmetic operations, the + symbol can also be used to concatenate strings, that is, to add one string to another. This is done by appending one string at the end of another. Here is an example:<%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim FirstName Dim LastName Dim FullName FirstName = "James " LastName = "Fame" FullName = FirstName + LastName %> <% Response.Write("Full Name: " & FullName) %> </body> </html>
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
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.
The integer division is performed using 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.
The decimal division is performed with the forward slash "/". Its formula is: Value1 / Value2 After the operation is performed, the result is a decimal number.
The exponentiation is is with the ^ operator (Shift + 6). It's formula is: 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. 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. |
|
|||||||||||||||||||||
|