Use classes in VBScript (1)

zhaozj2021-02-16  57

Use classes in VBScript

Foreword

First, in I entered a substantive topic and explain how to build a class, I hope to guarantee that "objects". 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?" - the object usually represents 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. E.g:

'Open the Database Connection

Set objconn = server.createObject ("adoDb.connection")

Objconn.open "MYDSN"

'Create the RecordSet Object

Set objrs = server.createObject ("adoDb.recordset")

'Define the SQL Query

StrcomplexSqlQuery = "Select C.Name, S.Name from Customers C," & _

"Salespeople S, Sales Sl where sl.customerid = C.ID AND" & _

"Sl.salespersonid = S.ID and sl. Iid =" & stridofthissale & ";"

'Open the recordset

Objrs.open strcomplexsqlQuery, Objconn, AdopenForwardonly, _

AdlockReadonly, AdcmdText

'Take The Customer and Sales Person Names from The Recordset

Strcustomername = ObJRS (0)

strsalespersonname = ObJRS (1)

'Tidy Up the Objects

Objrs.close

Objconn.close

Set objrs = Nothing

Set objconn = Nothing

'Output the data

Response.write "this Sale Was Made By" & StrasaSpersonName & _ "To" & structomername

You can use "objects" to replace:

'Create THE "

Sale

"Object

Set objsale = new sale

'Lookup the correct sales

Objsale.id = StridOfThissale

'Output the data

Response.write "this Sale Was Made By" & objsale.salespersonname & _

"To" & objsale.customername

'Tidy Up the Objects

Objsale.close

Set objsale = Nothing

If you use the "Sale" object to make more things than printing, you can save you a lot of typing 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.

If you have any questions, you are welcome to discuss in http://www.showc.com

Thank Sophie's translation

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

New Post(0)