SoFunction
Updated on 2025-04-13

Using classes in VBScript

First, before I get into the substantive topic and explain how to build a class, I want to make sure you know the "object". While you can use objects in your program without knowing the correct rules, I don't recommend this! For beginners of the object, the next section will give you an idea of ​​its concept and content. Readers who have already learned about object-oriented programming (OOP) can skip this chapter.
introduction
l "What is an object?" - Objects usually represent some kind of entity, mainly a collection of variables and functions.
l "What is an entity?"—Literally, an entity is a "thing", and I mean a concept or any object. For example, a car is an entity because it is an object. Your company's sales department sells products as an entity. Of course, you can also take it apart. Sales personnel, customers, products, etc. are all entities.
Let’s take a closer look at the entity (object) of “selling”. In order to make you have a sales "image" more accurately, you need to know what the customer bought, which customer is, who is the salesperson, etc.... This seems to be a simple event, but assuming that all the information is stored in a separate database table, then when you need to obtain all the relevant information of a sales process, you must do multiple independent queries in your database and then gather all the data. Is there an easier way to get all the information about the sale at once? "Object".
In an object, you can implant code to get data from other tables, and you can also save all information about object properties, so that you can easily manage your sales data using the code. For example:
''''''''Open the database connection
Set objConn = ("")
 "MyDSN"
''''''''Create the recordset object
Set objRS = ("")
''''''''Define the SQL query
strComplexSQLQuery = "SELECT ,  FROM Customers C, " & _
"Salespeople S, Sales Sl WHERE = AND " & _
"= AND =" & strIDOfThisSale & ";"
''''''''Open the recordset
 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


Set objRS = Nothing
Set objConn = Nothing
''''''''Output the data
 "This sale was made by " & strSalesPersonName & _
" to " & strCustomerName
You can use "object" instead:
''''''''Create the "Sale" object
Set objSale = New Sale
''''''''Lookup the correct sale
 = strIDOfThisSale
''''''''Output the data
 "This sale was made by " &  & _
" to " & 
''''''''Tidy up the objects

Set objSale = Nothing
If you use the "Sale" object to do more than print it, it can save you a lot of typing time.
In calculation, objects include "properties" and "methods". An attribute is mainly a variable stored in an object, and its usage is the same as a variable. The only difference is that the parameter assignment is: strMyVar = “This is a string variable”, and the object attribute is ="This is a string variable”. This is very simple and useful. The method can be understood as functions and procedures implanted in the object. You can use strMyVar = (strMyVar) instead of strMyVar = FunctionName(strMyVar). Different writing methods, but the functions are the same. An example of a property is ExpireAbsolute in the object Response, = CDate("1 September 1999"). An example of a method is the Write method in the object Response, "Hello world!".
A new feature of VBScript is that it can create new objects without requiring compilers that spend a lot of time. I will show the reader how to create an object's class and hopefully provide a good start.
Create an object
When creating object types (classes) in VBScript, you must first know that this is really easy! I taught myself one afternoon and just read the Microsoft VB Script reference book, but I must admit that this book is not the easiest document to read.
Beginners need to install the VBScript 5.0 engine, which can be downloaded at Microsoft'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Let's look at the code. The definition of a class is very similar to that of functions and subprocesses. The starting behavior is Class , ending with End Class, and all object definitions are written in the intermediate department. Now we can use what we have learned to create the first class, without implementing any functions.
Class 4GuysTestObject
End Class
This doesn't seem to be that way, but when you write the following code, you will create an instance of the object:
Dim objTestObject
Set objTestObject = New 4GuysTestObject
Set objTestObject = Nothing
Obviously, we can't do anything with objects yet, now I'll explain how to define properties and methods in objects.
The most basic thing that can be done with objects is to create a set of data. For example, if you want to build time, date and video program titles together, you can create an object that contains the properties "StartTime", "ProgramDate", and "ProgramTitle". The code is as follows:
Class TVProgram
Public StartTime
Public ProgramDate
Public ProgramTitle
End Class
Dim objTVShow
Set objTVShow = New TVProgram
 = CDate("17:30")
 = DateSerial(1999,9,17)
 = "The Jerry Springer Show"
  & " is on at " & _
 & " on " & 
The way the code works is that we define StartTime, ProgramDate, and ProgramTitle as properties of the TVProgram class. In this way, these properties are handled like other variables, and no code is executed without setting values. The Public field before the attribute name has its true meaning and is very important. If you do not specifically refer to a method or attribute as public or private, the system default value is public, but it is best to develop good writing habits to define any value (and it is also convenient for you to read it yourself later).
The results of the above program are roughly as follows (depending on your local server configuration):
The Jerry Springer Show is on at 5:30pm on 17/09/99.
I'm in the UK, so the date reality is as above. No matter what project you run, it will work well, but only when you start using the functions of other objects, creating a perfect interface for all the information and functions you may need to support the entities surrounding the objects you build, can you realize the real strength of the objects.
Now, if you don't like the method of displaying dates in the example above and want to realistic dates in the same format, and don't add FormatDateTime() when referencing each ProgramDate property, you just need to implant such code into the property itself.
This requires another method to define the properties. Again, we will use ProgramDate as an external visible property, but because the ProgramDate property will become a function instead of a static value, we will save the actual date in another property internal_ProgramDate.
Class TVProgram
Public StartTime
Public internal_ProgramDate
Public Property Get ProgramDate
ProgramDate = Day(internal_ProgramDate) & _
" " & MonthName(Month(internal_ProgramDate)) & _
" " & Year(internal_ProgramDate)
End Property
Public ProgramTitle
End Class
Dim objTVShow
Set objTVShow = New TVProgram
 = CDate("17:30")
objTVShow.internal_ProgramDate = DateSerial(1999,9,17)
 = "The Jerry Springer Show"
  & " is on at " & _
 & " on " &  & "."
The results of the program are as follows:
The Jerry Springer Show is on at 5:30pm on 17 September 1999. 
Let’s analyze the procedures in (2):
Class TVProgram
Public StartTime
Public internal_ProgramDate
Public Property Get ProgramDate
ProgramDate = Day(internal_ProgramDate) & _
" " & MonthName(Month(internal_ProgramDate)) & _
" " & Year(internal_ProgramDate)
End Property
Public ProgramTitle
End Class
Dim objTVShow
Set objTVShow = New TVProgram
 = CDate("17:30")
objTVShow.internal_ProgramDate = DateSerial(1999,9,17)
 = "The Jerry Springer Show"
  & " is on at " & _
 & " on " &  & "."
When the object's property ProgramDate is called, the function ProgramDate is actually executed, that is, the function defined above, and soon you will get used to this way of using the Public or Private keywords in the declaration part. The keyword "Property" tells the compiler to call functions externally just like calling properties. The next "Get" indicates whether the function outputs or obtains a value.
Get means "allow external code to 'get' a value". Similar keywords include "Let" and "Set", but these two are more complicated, so we will discuss them later.
The next code seems a bit difficult, assign a value to objectname.internal_ProgramDate and call it through. Wouldn't it be better if it could be used to assign a value to it and get its value at the same time? Of course, that's OK.
If the name of the definition Get and Let attributes are the same, they can be treated as the same properties of the object, but this is limited to them defining the same number of members. (The following code looks different, only for instance reference)
Class TVProgram
Public StartTime
Public internal_ProgramDate
Public Property Get ProgramDate
ProgramDate = Day(internal_ProgramDate) & " " _
& MonthName(Month(internal_ProgramDate)) & _
" " & Year(internal_ProgramDate)
End Property
Public Property Let ProgramDate(ByVal varDateIn)
internal_ProgramDate = CDate(varDateIn)
End Property
Public ProgramTitle
End Class
Dim objTVShow
Set objTVShow = New TVProgram
 = CDate("17:30")
 = "17 Sept 99"
 = "The Jerry Springer Show"
  & " is on at " & _
 & " on " &  & "."
The declaration part of Let in the above code seems to be a redundant element that I studied for a long time when I first saw it. Every time I use "0" as a variable on each property, I always get this error message, "The number of elements must be equal". "They are indeed equal!" After going crazy, I turned around and looked at the program and felt stupid! :)
The reason is that when you try to assign a value to ProgramDate, you will use a line like this:
 = dtmMyDate
For convenience, the value on the right side of the equal sign (here refers to dtmMyDate) is assigned to the function as a program element. Therefore, the compiler may think that there are 0 program companies in the Get ProgramDate line, but there is one more Let ProgramDate! The assigned value is always skipped and used as the last element of the property, so even if you use other element, the assigned value is always the last element.
Now look at the program. Whether you set the date to text through ProgramDate or translate it into a date variable with internal_ProgramDate, there is no problem with the program. But can you only use one entrance?
If internal_ProgramDate can only be valid internally, and using Let ProgramDate to check the transmitted data type, we can make a choice. For example:
Class TVProgram
Public StartTime
Private internal_ProgramDate
Public Property Get ProgramDate
ProgramDate = Day(internal_ProgramDate) & " " & _
MonthName(Month(internal_ProgramDate)) & _
" " & Year(internal_ProgramDate)
End Property
Public Property Let ProgramDate(ByVal varDateIn)
If IsDate(varDateIn) Then
internal_ProgramDate = varDateIn
Else
''''''''Place some error handling code in here.
End If
End Property
Public ProgramTitle
End Class
And declare the StartTime property as well:
Class TVProgram
Private internal_StartTime
Public Property Get StartTime
StartTime = Hour(internal_StartTime) & ":" _
& Minute(internal_StartTime)
End Property
Public Property Let StartTime(ByVal varTimeIn)
If IsDate(varTimeIn) Then
internal_StartTime = varTimeIn
End If
End Property
Private internal_ProgramDate
Public Property Get ProgramDate
ProgramDate = Day(internal_ProgramDate) & " " _
& MonthName(Month(internal_ProgramDate)) & _
" " & Year(internal_ProgramDate)
End Property
Public Property Let ProgramDate(ByVal varDateIn)
If IsDate(varDateIn) Then
internal_ProgramDate = varDateIn
End If
End Property
Public ProgramTitle
End Class
...
The code is still somewhat less practical than what we want. We will use the TVProgram class on other pages, so it is best to define it independently so that all the pages can be called. We will discuss this in Part 4
The code is still somewhat less practical than what we want. We will use the TVProgram class on other pages, so it is best to define it independently so that all the pages can be called. Create an ASP page and name it, where we define the class TVProgram.
----
<%
       Class TVProgram
              Private internal_StartTime
              Public Property Get StartTime
                      StartTime = Hour(internal_StartTime) & _
                              ":" & Minute(internal_StartTime)
              End Property
              Public Property Let StartTime(ByVal varTimeIn)
                      If IsDate(varTimeIn) Then
                              internal_StartTime = varTimeIn
                      End If
              End Property
              Private internal_ProgramDate
              Public Property Get ProgramDate
                      ProgramDate = Day(internal_ProgramDate) & _
                              " " & MonthName(Month(internal_ProgramDate)) & _
                              " " & Year(internal_ProgramDate)
              End Property
              Public Property Let ProgramDate(ByVal varDateIn)
                      If IsDate(varDateIn) Then
                              internal_ProgramDate = varDateIn
                      End If
              End Property
              Public ProgramTitle
       End Class
%>
This way we can call the class we defined in any ASP, with the syntax as follows:
<!-- #include virtual="" -->
<%
       Dim objTVShow
       Set objTVShow = New TVProgram
        = CDate("17:30")
        = DateSerial(1999,9,17)
        = "The Jerry Springer Show"
%>
<%=  %> is on at <%=  %> on <%=  %>.
Here is a suggestion. If you rename your included file.asp and ensure that all important codes are in <CODE><% ...%>< CODE>, then even if someone guesses the file name of your included file, he will not be able to see the content inside!