Home

Details on Using Variables

 

Constants

 

Introduction

A constant is a value that doesn't change. There are two types of constants you will use in your programs: those supplied to you and those you define yourself.

To create a constant to use in your program type the Const keyword followed by a name for the variable, followed by the assignment operator "=", and followed by the value that the constant will hold. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Const DateOfBirth = #12/5/1974#
%>

<%
    Response.Write(DateOfBirth)
%>

</body>
</html>

When defining a constant like this, you would know the type of data to apply to the variable. In this case the DateOfBirth constant holds a Date value. Still, to be more explicit, you can indicate the type of value of the constant by following its name with the As keyword and the desired data type. Based on this, the above program would be:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Const DateOfBirth As Date = #12/5/1974#
%>

<%
    Response.Write(DateOfBirth)
%>

</body>
</html>

When creating a constant, if its value supports a type character, instead of using the As keyword, you can use that type character. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Const Temperature% = 52
%>

</body>
</html>

The second category of constants are those that are built in the Visual Basic language. Because there are many of them and used in different circumstances, when we need one, we will introduce and then use it.

Nothing

So far, to initialize a variable, we were using a known value. Alternatively, you can use the Nothing constant to initialize a variable, simply indicating that it holds a value that is not (yet) defined. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim DateOfBirth As Date = Nothing
%>

</body>
</html>

If you use the Nothing keyword to initialize a variable, the variable is actually initialized to the default value of its type. For example, a number would be assigned 0, a date would be assigned January 1, 0001 at midnight.

 
 
 
 

Details on Declaring Variables

 

Declaring a Series of Variables

Because a program can use different variables, you can declare each variable on its own line. Here are examples:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim NumberOfPages As Integer
    Dim TownName As String
    Dim MagazinePrice As Double
%>

</body>
</html>

It is important to know that different variables can be declared with the same data type as in the following example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim NumberOfPages As Integer
        Dim Category As Integer
        Dim MagazinePrice As Double
%>

</body>
</html>

When two variables use the same data type, instead of declaring each on its own line, you can declare two or more of these variables on the same line. There are two techniques you can use:

  • You can write the names of both variables on the same line, separated by a comma, and ending the line with the common data type:
     
    <%@ Page Language="VB" %>
    
    <html>
    <head>
    
    <title>Exercise</title>
    
    </head>
    <body>
    
    <%
            Dim NumberOfPages, Category As Integer
            Dim MagazinePrice As Double
    %>
    
    </body>
    </html>

    In this case, both variables NumberOfPages and Category, are the same type, Integer

  • You can also declare, on the same line, variables that use different data types. To do this, type the Dim keyword followed by the variable name and its data type with the As keyword. Then type a comma, followed by the other variable(s) and data type(s). Here is an example
     
    <%@ Page Language="VB" %>
    
    <html>
    <head>
    
    <title>Exercise</title>
    
    </head>
    <body>
    
    <%
            Dim NumberOfPages, Category As Integer
            Dim CountryName As String, MagazinePrice As Double
    %>
    
    </body>
    </html>

    In this case, CountryName is a string variable while MagazinePrice is a Double variable

You can use the same techniques when declaring many global variables. After declaring the variables, you can initialize and use them as you see fit.

Read-Only Variables

We have indicated that when a variable is declared, it receives a default initialization unless you decide to specify its value. Whether such a variable has been initialized or not, at any time, you can change its value by reassigning it a new one. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    ' Initializing a variable when declaring it
    Dim Number As Double = 155.82
    Response.Write("Number: " & Number)

    ' Changing the value of a variable after using it
    Number = 46008.39
    Response.Write("<br>Number: " & Number)
%>

</body>
</html>

An alternative of creating a constant is to specify that a variable is read-only. To do this, use the ReadOnly keyword. While a constant variable can be declared locally, a ReadOnly variable cannot. It must be declared globally. 

 
 
   
 

Previous Copyright © 2009-2013 FunctionX, Inc. Next