Using classes in VBScript (i)

Source: Internet
Author: User
Tags object string variable tidy
Vbscript|vbscript Preface

First, before I go into the substantive topic and explain how to create a class, I want to make sure you know "object." Although you can use objects in a program without having to know the correct rules, I don't recommend it! For beginners of the object, the next section will let you understand its concepts and content. Readers who already understand object-oriented programming (OOP) can skip this chapter.



Introduction

L "What is the object?" "--an object usually represents an entity, primarily a collection of variables and functions.

L "What is the entity?" "--literally, the entity is a" thing, "I mean a concept or any object. For example, a car is an entity because it is an object. Sales of your company's sales department is also an entity, of course, you can also take it apart, sales people, customers, products, etc. are entities.



Let's look at the "Sales" Entity (object) in more depth. In order to make you more accurate to have a sales "image", you need to know what the customer bought, which customer, who is the salesperson and so on ... This seems like a simple event, but assuming all the information is stored in a separate database table, then when you need to get all the relevant information about a sales process, you have to do multiple independent queries in your database and gather all the data. Is there a more convenient way to get all the information about the sale at once? "Object".

In an object, you can embed code to get data from other tables, and you can save all the information about the object's properties so that you can easily manage your sales data using code. For example:

' 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.id=" & Stridofthissale & ";"

' Open ' 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 is made by" & Strsalespersonname & _
"To" & Strcustomername


You can use objects to override the following:

' Create the ' Sale ' object
Set Objsale = New Sale

' Lookup the correct sale
Objsale.id = Stridofthissale

' Output the data
Response.Write "This sale is made by" & Objsale.salespersonname & _
"To" & Objsale.customername

' Tidy up the objects
Objsale.close
Set Objsale = Nothing
If you use "Sale" objects to do more than print, you can save a lot of typing time.



In calculations, the object includes properties and methods. A property is primarily a variable stored in an object, with the same usage as a variable. The only difference is that the parameter assignment is: Strmyvar = ' This is a string variant ' and the object property is Objobject.property= ' This is a string variant '. This is a very simple and useful place. Method can be understood as the function and process in the implanted object, and the Strmyvar = Objobject.methodname (Strmyvar) can be used instead of Strmyvar =functionname (Strmyvar). The writing is different, but the function is the same. An example of a property is the Expireabsolute,response.expiresabsolute = CDate in an object Response ("1 September 1999"). An example of the method is the Write method in Object Response, Response.Write "Hello world!".

A new feature of VBScript is that it can create new objects without requiring a compiler that is extremely time-consuming. I'll show my readers how to create classes for objects and hopefully provide a good start.



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

Thanks for Sophie's translation



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.