Using Object-Oriented Programming with VBScript - Designing Active Server Pages [Book]
There are two kinds of procedures in VBScript: A sub procedure and a function. . There is no relationship between the arguments; for example, they can be of. A real-world example of an object/instance relationship can be seen with automobiles. Furthermore, there is no support for inheritance in VBScript. . This public method would expect one parameter: the value of the phone number the end. There is no duplication of efforts between the VBScript compatible functions. The VBScript The number argument can be any valid numeric expression.
Rather, it simply serves as a template for creating instances. An instance is a physical entity of an object. Connection" You must have an instance of an object to make any method calls or to set or get any properties. A Mercury Sable GS is an instance of a car, able to have its properties modified and its methods implemented. Encapsulating complexity Object-oriented programming can be used to encapsulate the complexity associated with particular tasks.
For example, imagine that on your web site you needed to be able to open a text file, count the number of lines in the file, and then log that information in another file.
Open a particular text file. Count the number of lines in that text file. Close the text file. Open up the logging file. Log the number of lines from the file in Step 2.
Close the log file. Granted, these steps are not complex or too lengthy, but imagine that a novice developer is expected to perform these steps in various ASP pages. We can simplify the process by encapsulating the above steps into a single object.
LogTextFileLines will also contain one method, UpdateLog, which is responsible for counting the number of lines in a specified text file and recording the line count in the appropriate log file. UpdateLog will have the following definition: Function UpdateLog strFileName where strFileName is the path to the text file whose lines need to be counted and logged. UpdateLog could return a Boolean value to indicate whether or not the operation was successful.
Once this object is created, the six-step task outlined earlier becomes much simpler: Write "There was an error when attempting to log C: Furthermore, there is no support for inheritance in VBScript.
Tip Inheritance is an OOP technique used to group objects into a logical hierarchy corresponding to the relationships among objects. For example, if you had a Mammal object, two of its children in this hierarchy might be the Canine and Feline objects. These latter two objects were inherited from Mammal.
As an inherited class, Canine and Feline would have the basic methods and properties of a Mammal, as well as their own unique, specialized methods and properties. When creating classes, keep in mind you are creating a tool to be used by other developers.
Differences Between Parameters and Arguments (Visual Basic) | Microsoft Docs
In the discussion of classes throughout this book, there will be certain times when a distinction between the developer who created the class and the developer who is using the class is needed.
The developer who created a class for use by other developers will be referred to as the class developerwhile the developer using the created class will be referred to as the end developer. Rather than force the end developers to cut and paste a class definition into each ASP page they use that needs that particular class definition, it is best to create a text file that contains the class definition.
Then any end developer who needs to use a class can simply use a server-side include to import the class definition into their ASP page.
Designing Active Server Pages by Scott Mitchell
For example, if you wanted to add a new feature to an existing class, make sure that when adding it existing code utilizing that class will not break! That way, old code still functions. Creating classes To create a class, use the Class statement, which has the following syntax: Class ClassName 'Define the class's properties and methods here End Class where ClassName is the name you choose to assign to your class; your name must follow standard VBScript variable naming conventions.
The Initialize and Terminate events When creating classes, there are two important event handlers to be aware of: The Initialize event fires when a class instance is created using the New keyword. For example, we could create an instance of the SomeObject class using: We can define these event handlers in our classes. Properties are variables that the end developer can set that determine the state of the class.
Methods are functions of the class that the end developer can call to have the class perform a given task. Connection object contains properties that describe the state of the object instance, such as ConnectionString, ConnectionTimeout, and Provider, and methods that perform actions, such as Open and Close. A class, though, can contain variables and functions that the end developer cannot directly call.
The terminology may seem a bit confusing, or at least overly verbose. To put it another way, a class contains functions and variables. If the end developer can call a class function, the function is referred to as a method; otherwise, it is referred to as a member function.
Differences Between Parameters and Arguments (Visual Basic)
Similarly, if the end developer can call a class variable, it is referred to as a property ; otherwise, it is referred to as a member variable. The public and private statements As discussed earlier in Section 4. To assist with this encapsulation of complexity, VBScript allows you to hide methods and properties of an object. Remember, an object should serve as a black box for the end developer; as the creator of an object, you may wish to prevent the end user from directly calling specific methods or setting certain properties.
To create member functions and member variables methods and properties that are hidden from the end developerprecede the member variable or member function definition with the Private keyword. Tip Note that the Initialize and Terminate event handlers in Example 4. This prevents the end developer from explicitly triggering these events.
To create a property or method, precede the variable or function definition with the Public keyword. The following code creates a class with a public method and property and a private method and property: Sub ShowMeTheDough End Sub The name of a sub procedure should follow the same rules we reviewed for the the variables, omitting the prefix: If the sub procedure performs an action that can be represented with a verb, you can use that verb to name it.
Examples are Show, Play, Dispose, Close You should use explicit names that identify the purpose of the sub 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. If the name of a procedure is a combination of words, start each word in uppercase.
It retrieves fields of two text boxes first name and last name on a form and displays a full name as a result of combining them: In the same way, you can declare variables in the procedure if you need to. These variables are declared and dealt with in the same way we learned in the regular script sections. Using declared variables, the above procedure can be written as follows: To call a simple procedure such as the earlier DisplayFullName, you can just write the name of the sub procedure.
In the following example, the above DisplayFullName sub procedure is called when the user clicks the Detail section of the form: Sub Detailer DisplayFullName End Sub If you want the procedure to be accessed immediately as soon as the page displays, you can assign its name to the onLoad event of the body tag.
Passing an Argument To carry an assignment, sometimes a procedure needs one or more values to work on. If a procedure needs a variable, such a variable is called an argument. Another procedure might need more that one argument, thus many arguments. The number and types of arguments of a procedure depends on various factors.
If you are writing your own procedure, then you will decide how many arguments your procedure would need. You also decide on the type of the argument s. For a procedure that is taking one argument, in the parentheses of the procedure, write a name for the argument. Here is an example: If you are creating such a procedure, between the parentheses of the procedure, write the name of the first argument followed by a comma; add the second argument and subsequent arguments and close the parentheses.
- INTERACT FORUM
There is no relationship between the arguments; for example, they can be of the same type: Actually, there are various ways you can call a sub procedure. As we saw already, if a sub procedure doesn't take an argument, to call it, you can just write its name. If a sub procedure is taking an argument, to call it, type the name of the sub procedure followed by the name of the argument.
If the sub procedure is taking more than one argument, to call it, type the name of the procedure followed by the name of the argument, in the exact order they are passed to the sub procedure, separated by a comma.
In this case, when calling a procedure using Call, you must include the argument s between the parentheses.