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