Home

Introduction to Operations on a Web Page

 

Values Fundamentals

 

Introduction

When a visitor is presented with a web page that is equipped with controls that expect values, the visitor may be given various options, such as typing a name, entering a number, or selecting from a list. In any case, the visitor would be creating and submitting a value. As the page developer, you can use that value.

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.

Practical Learning: Introducing Variables

  1. Start Microsoft Visual Web Developer or Microsoft Visual Studio
  2. To create a new web page, on the main menu, click File -> New -> Web Site... or File -> New Web Site...
  3. Set the Language to Visual Basic
  4. Set the name (at the end of the Location) to variables1
  5. Click OK

Declaring a Variable

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>

Practical Learning: Declaring Variables

  1. To declare some variables, change the file as follows:
    <%@ Page Language="VB"
        AutoEventWireup="false" 
        CodeFile="Default.aspx.vb" 
        Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Yugo National Bank</title>
    </head>
    <body>
    
    <%
        Dim CustomerName
        Dim AccountNumber
        Dim InitialDeposit
    %>
    </body>
    </html>
  2. Save the file

The Name of a Variable

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:

  • The name of a variable can consist of only one a letter (a, b, c, d, e, f, g, h, i, f, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z. A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, or Z)

  • The name of a variable can start with with a letter

  • The name of a variable can start with with an underscore _.
    If the first character is an underscore, it must be followed by a letter

  • After the first character that is a letter or an underscore, the name can contain letters, underscores, or digits (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9)

  • The name cannot have an empty space

  • The name cannot have any special character (! @ # $ % ^ & * + - = ~ ` < > , . : ; " ' { [ } ] | \ ) between letters or digits except the underscore as specified above. Some characters can be used as the last character of a name. We will encounter them when necessary

  • Can have up to 255 characters but refrain from using a name that is too long (more than 64 characters)

  • Must be unique inside of the procedure or the module it is used in

A name cannot be one of the following reserved words:

AddHandler AddressOf And As Boolean
ByRef Byte ByVal Call Case
Catch CBool CByte CChar CDate
CDbl CDec Char CInt Class
CLng CObj Const Continue
CSByte CShort CSng CStr CType
CUInt CULng CUShort Date Decimal
Delegate Dim
Do Double Each Else ElseIf
End EndIf Enum Equals Erase
Error Event Exit Explicit False
Finally For Friend From Function
Get GetType GetXmlNamespace GoSub GoTo
Group By Group Join Handles If Implements
Imports In Inherits Integer Interface
Into Is IsFalse IsNot IsTrue
Join Key Let Lib Like
Long Loop Me Mid Mod
Module MustInherit MustOverride MyBase MyClass
Namespace Narrowing New Next Not
Nothing NotInheritable NotOverridable Object Of
Off On Operator Option Optional
Or Overloads Overridable Overrides ParamArray
Partial Preserve Private
Property Protected Public RaiseEvent ReadOnly
ReDim REM RemoveHandler Resume Return
SByte Select Set Shadows Shared
Short Single Skip Skip While Static
Step Stop Strict String Structure
Sub SyncLock Take Take While Text
Then Throw To True Try
TryCast TypeOf UInteger ULong Unicode
UShort Until Using Varint
Went When Where While Widening
With WithEvents WriteOnly Xor

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.

Option Explicit

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

Values Fundamentals

 

Introduction

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.

Value Conversion

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.

Requesting a Value

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: ")

Displaying a Value

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>

Operations on Values

 

Introduction

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.

The Line Continuation Operator: _

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.

The Parentheses: ()

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 ,

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

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.

The Double Quotes: ""

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>

Practical Learning: Using Double-Quotes

  1. To use double-quotes and request some values, change the file as follows:
    <%@ Page Language="VB"
        AutoEventWireup="false" 
        CodeFile="Default.aspx.vb" 
        Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Yugo National Bank</title>
    </head>
    <body>
    
    <%
        Dim CustomerName
        Dim AccountNumber
        Dim InitialDeposit
    
        CustomerName = InputBox("Enter Customer Name:")
        AccountNumber = InputBox("Enter Customer Acnt #:")
        InitialDeposit = InputBox("Enter Initial Deposit:")
    %>
    
    <%
        Response.Write("<p><b>Yugo National Bank</b></p>")
        Response.Write("Customer Name: ")
        Response.Write(CustomerName)
        Response.Write("<br>Account Number: ")
        Response.Write(AccountNumber)
        Response.Write("<br>Initial Deposit: ")
        Response.Write(InitialDeposit)
    %>
    </body>
    </html>
  2. To test the program, press Ctrl + F5
  3. Enter the requested value as:
     
    Enter Customer Name: Gertrude Monay
    Enter Customer Acnt #: 92-37293-30
    Enter Initial Deposit: 450.00
  4. When you have finished, return to your programming environment

The Colon Operator :

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>

String Concatenation: &

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.

Practical Learning: Concatenating Some Strings

  1. To use the string concatenator, change the file as follows:
    <%@ Page Language="VB"
        AutoEventWireup="false" 
        CodeFile="Default.aspx.vb" 
        Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Yugo National Bank</title>
    </head>
    <body>
    
    <%
        Dim CustomerName
        Dim AccountNumber
        Dim InitialDeposit
    
        CustomerName = InputBox("Enter Customer Name:")
        AccountNumber = InputBox("Enter Customer Acnt #:")
        InitialDeposit = InputBox("Enter Initial Deposit:")
    %>
    
    <%
        Response.Write("<p><b>Yugo National Bank</b></p>")
        Response.Write("Customer Name: " & CustomerName)
        Response.Write("<br>Account Number: " & AccountNumber)
        Response.Write("<br>Initial Deposit: " & InitialDeposit)
    %>
    </body>
    </html>
  2. Save the file
  3. Return to the browser and refresh it
  4. Enter the same values as previously
  5. Return to your programming environment
 
 
 
 

Arithmetic Operators

 

Addition +

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>

Practical LearningPractical Learning: Using the Addition Operator

  1. To use the addition, change the file as follows:
    <%@ Page Language="VB"
        AutoEventWireup="false" 
        CodeFile="Default.aspx.vb" 
        Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Yugo National Bank</title>
    </head>
    <body>
    
    <%
        Dim CustomerName
        Dim AccountNumber
        Dim InitialDeposit
        Dim RegularDeposit
        Dim TotalDeposits
    
        CustomerName = "Paul Bertrand Yamaguchi"
        AccountNumber = "52-92074-95"
        InitialDeposit = 325
        RegularDeposit = 750
        TotalDeposits = InitialDeposit + RegularDeposit
    %>
    
    <%
        Response.Write("<p><b>=-= Yugo National Bank =-=</b></p>")
        Response.Write("Customer Name:   " & CustomerName)
        Response.Write("<br>Account Number:  " & AccountNumber)
        Response.Write("<br>Total Deposits: $" & TotalDeposits)
    %>
    </body>
    </html>
  2. Save the file
  3. Return to the browser and refresh
     
    Addition
  4. Return to your programming environment

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

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 LearningPractical Learning: Using the Subtraction Operator

  1. To subtract, change the file as follows:
    <%@ Page Language="VB"
        AutoEventWireup="false" 
        CodeFile="Default.aspx.vb" 
        Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Yugo National Bank</title>
    </head>
    <body>
    
    <%
        Dim CustomerName, AccountNumber
        Dim InitialDeposit, Withdrawals
        Dim RegularDeposit, TotalDeposits
        Dim CurrentBalance
    
        CustomerName = "Henriette Jolana"
        AccountNumber = "84-48662-72"
        InitialDeposit = 1500
        RegularDeposit = 468.75
        TotalDeposits = InitialDeposit + RegularDeposit
        Withdrawals = 300
        CurrentBalance = TotalDeposits - Withdrawals
    %>
    
    <%
        Response.Write("<p><b>=-= Yugo National Bank =-=</b></p>")
        Response.Write("Customer Name:   " & CustomerName)
        Response.Write("<br>Account Number:  " & AccountNumber)
        Response.Write("<br>Total Deposits: $" & TotalDeposits)
        Response.Write("<br>Withdrawals:    $" & Withdrawals)
        Response.Write("<br>Current Balance: $" & CurrentBalance)
    %>
    </body>
    </html>
  2. Save the file
  3. Refresh the browser
     
    Subtraction
  4. Return to your programming environment

Integer Division \

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.

Decimal Division /

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.

Exponentiation ^

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.

Remainder: Mod

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.

 
 
   
 

Home Copyright © 2009-2013 FunctionX, Inc. Next