Use classes in VBScript (1)

zhaozj2021-02-16  95

Use classes in VBScript

Introduction First, I want to guarantee that "Objects" before I entered a substantive topic and explain how to build a class. Although you can use objects in the program without knowing its correct rules, I don't recommend this! For beginners, the next part will let you understand its concept and content. Readers who have been known to object-oriented programming (OOP) can skip this chapter.

Introduction L "What is the object?" - Objects usually represent a certain entity, mainly a collection of variables and functions. l "What is the entity?" - said on the literal, the entity is a "thing", I mean a concept or any object. For example, a car is an entity because it is an object. Your company's sales department selling products is also an entity. Of course, you can also open it, sales staff, customers, products, etc. are entities.

Let us see "sales" entity (object). In order to make you more accurately have a sales "image", you need to know what customers bought, which customer, who is sales staff, etc. ... This seems to be a simple event, but all information is stored In a separate database table, then when you need to get all relevant information about a sales process, you must do multiple independent queries in your database, and then close all the data. Is there any more convenient way to get some information for sale at a time? "Object". In the object, you can implant the code to get data from other tables, you can also save all information about the object properties so you can easily use the code to manage your sales data. For example: 'open the database connection set Objconn = server.createObject ("adodb.connection") Objconn.Open "MyDSN"

'Create the RecordSet ObjectSet Objrs = Server.createObject ("AdoDb.Recordset")

'Define the squery = "Select c.name, s.name from customer s, sales sl where sl.customerid = C.ID and" & _ "sl.salespersonid = S.ID and SL .Id = "& stridOfthissale &"; "

'Open the recordsetobjrs.open strcomplexsqlquery, objconn, adopenforwardonly, _ AdlockReadonly, AdcmdText

'Take the customer and sales person name from the recordstrcustomername = objrs (0) strsalespersonname = objrs (1)

'Tidy Up The Objectsobjrs.closeobjconn.closset Objrs = NothingSet Objconn = Nothing

'Output the dataresponse.write "this sale wage" & strs "to" & strcustomername can replace "object" to replace:' create the "

Sale

"Objectset Objsale = New Sale

'Lookup the correct saleobjsale.id = StridOfThissale

'Output the dataresponse.write "this sales master" & objsale.salespersonname & _ "to" & objsale.customername

'Tidy Up The ObjectSobjsale.closset Objsale = Nothing If you use the "Sale" object to make you more typographic time.

In the calculation, the object includes "attributes" and "method". The attribute is mainly a variable stored in the object, which is the same as the variable. The only difference is that the parameter assignment is: strmyvar = "this is a string variant", and the object attribute is objobject.property = "this is a string variant". This is very simple and useful. Methods can be understood as functions and processes in implanted objects, which can use strmyvar = objObject.methodName (strmyvar) instead of strmyvar = functionname. Different, but the function is the same. An example of an attribute is an ExpireabSolute, response.expiresabsolute = cdate ("1 September 1999") in object response. An example of a method is the Write method in the object response, response.write "Hello World!".

A new feature of VBScript is that it can create a new object without requiring a compiler that is very large. I will show the reader how to create an object class and want to provide a good start.

转载请注明原文地址:https://www.9cbs.com/read-12993.html

New Post(0)