Home

Introduction to Procedures

  

Introduction

A procedure is a relatively small section of code meant to produce a result. This make it possible to segment a program in different parts that each accomplishes a specific purpose. The Visual Basic language supports normal procedures and functions.

There are various types of procedures and functions you will use in your web pages. In this lesson, we will learn how to create our own procedures. Another category of procedures, mostly functions, involve those that have already been created. They are part of the Visual Basic language. Eventually, we will also study some of them.

There are various ways you can create a procedure. You can create it in a separate file or you can make it part of the web page where it will be used.

Using a Script

If you want to create a procedure in the web page where you plan to use it, include it in the head section of the web page. The creation of a procedure starts with <script> and ends with </script>:

<%@ Page Language="VB" %>

<html>
<head>

<script>

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

The <script> tag uses various attributes we will review. To start, you must specify that the script will run on the server. To do this, at the attribute runat to the script and assign the server string to it. This can be done as follows:

<%@ Page Language="VB" %>

<html>
<head>

<script runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

After doing this, you can create your procedure between the starting <script> and the end </script> tags.

When creating a procedure, the browser assumes that you will follow Visual Basic rules. When creating a procedure, you can (sometimes must) specify the language you plan to use. To support this, the <script> tag is equipped with an attribute named language. To specify the language, assign VB, VBSscript, JavaScript, JScript, or ECMAScript (remember that the Visual Basic language is not case-sensitive). This can be done as follows:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

If you don't specify the language, VB is assumed. In most cases, VB and VBScript follow the same rules (but they have some slight differences). JavaScript and JScript don't follow the same rules. For this reason, if you plan to follow rules other than those of VB, you should specify the language.

Besides the language, you should specify how the code of your script will be formatted or considered. To support this, the <script> tag provides the type attribute. To specify it, assign:

  • text/VB to the type attribute if you had, or will, specify the language as VB
  • text/vbsscript to the type attribute if you had, or will, specify the language as VBScript
  • text/javascript to the type attribute if you had, or will, specify the language as JavaScript
  • text/jscript to the type attribute if you had, or will, specify the language as JScript
  • text/ecmascript to the type attribute if you had, or will, specify the language as ECMAScript

Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

Remember that only the runat attribute is required. The others are optional. After specifying the values of the desired attributes, you can create your procedure.

 
 
 
 

 

Creating a Sub Procedure

A sub procedure is an assignment that is carried but does not give back a result. To create a sub procedure, start by typing the Sub keyword followed by a name. The name of a procedure is always followed by parentheses. At the end of the sub procedure, you must type End Sub. Therefore, the primary syntax of a sub procedure is:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Sub ProcedureName()

End Sub

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

The name of a procedure should follow the same rules we learned to name the variables. In addition:

  • If the procedure performs an action that can be represented with a verb, you can use that verb to name it. Here are examples: show, display
  • To make the name of a procedure stand, you should start it in uppercase. Examples are Show, Play, Dispose, Close
  • You should use explicit names that identify the purpose of the procedure. If a procedure would be used as a result of another procedure or a control's event, reflect it on the name of the sub procedure. Examples would be: afterupdate, longbefore.
  • If the name of a procedure is a combination of words, you should start each word in uppercase. Examples are: AfterUpdate, SayItLoud

The section between the Sub and the End Sub lines is referred to as the body of the procedure. Here is an example:

<script language="vbscript" type="text/vbsscript" runat="server">

Sub Assign()

End Sub

</script>

The body of the procedure is used to define what, and how, the assignment would be carried. For example, if you need to use a variable, you can declare it and specify the kind of variable you need. There is no restriction on the type of variables that can be declared in a procedure. Here is an example in which a string variable is declared in the body of a sub routine:

<script language="vbscript" type="text/vbsscript" runat="server">

Sub Assign()
    Dim strFullName As String
End Sub

</script>

In the same way, you can declare as many variables as you need inside of a procedure. The actions you perform inside of a procedure depend on what you are trying to accomplish. For example, a procedure can simply be used to create a string. The above procedure can be changed as follows:

<script language="vbscript" type="text/vbsscript" runat="server">

Sub Assign()
    Dim strFullName As String
    strFullName = "Paul Bertrand Yamaguchi"
End Sub

</script>

Calling a Sub Procedure

Once you have a procedure, whether you created it or it is part of the Visual Basic language, you can use it. Using a procedure is also referred to as calling it. To call a simple procedure, type its name followed by parentheses in the section where you want to use it. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Sub Assign()
    Dim strFullName As String
    strFullName = "Paul Bertrand Yamaguchi"

    Response.Write(strFullName)
End Sub

</script>

<title>Exercise</title>

</head>
<body>

<%
    Assign()
%>
</body>
</html>

This would produce:

Procedure

Besides using the name of a procedure to call it, you can also precede it with the Call keyword. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Sub Assign()
    Dim strFullName As String
    strFullName = "Alain"

    Response.Write(strFullName)
End Sub

</script>

<title>Exercise</title>

</head>
<body>

<%
    Call Assign()
%>
</body>
</html>

Procedures and Access Modifiers

A procedure can use access modifiers. A procedure can be made a private procedure, a friendly procedure, or a public procedure, using the Private, the Friend, or the Public keywords respectively:

  • Private: A procedure marked as private can be called only from the same file
  • Friend: A friendly procedure can be called from other files of the same web site
  • Public: A public procedure can be called from its web site and other web sites on the same server
 
 
   
 

Home Copyright 2009 FunctionX Next