Using classes in VBScript (ii)

Source: Internet
Author: User
Tags date define object end
Vbscript|vbscript Create objects



When you create an object type (Class) in VBScript, you first have to know that it's really easy! I self-study in an afternoon, just read the Microsof VB Script reference book, but 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 the Microsoft ' s scripting site.

Let's look at the code. The definition of a class is very similar to a function and a child procedure. The Start behavior class <myclassname&gt, ending with End class, all object definitions written in the intermediary department. Now we can build the first class with what we've learned and not implement any functional classes.

Class 4GuysTestObject

End Class



This doesn't seem to be the case, but when you write the following code, you create an instance of the object:

Dim Objtestobject

Set Objtestobject = New 4GuysTestObject

Set Objtestobject = Nothing



Obviously, we can't do anything with objects right now, and now I'm going to explain how to define properties and methods in the object.

The most basic thing you can do with an object is to create a set of data. For example, if you want to set up a time, date, and video program title, you can create an object that contains the attributes "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



Objtvshow.starttime = CDate ("17:30")

Objtvshow.programdate = DateSerial (1999,9,17)

Objtvshow.programtitle = "The Jerry Springer Show"



Response.Write Objtvshow.programtitle & "are on" & _

Objtvshow.starttime & "on" & Objtvshow.programdate



The way the code works is that we define the properties of Starttime,programdate and Programtitle as Class Tvprogram. In this way, these properties are processed like other variables, and the code is not executed without a set value. The public field before the property name has its true meaning and is very important. If you do not specifically refer to a method or property as public or private, the system defaults to public, but it is best to develop a good writing habit that defines any value (and also facilitates your own reading later).

The results of the above program are roughly as follows (depends on your local server configuration):

The Jerry Springer show are on 17/09/99 in 5:30pm.



I am in England, so the date is realistic as above. No matter what project you run, it works fine, but only if you start using the functions of other objects, create a perfect interface for all the information and functionality you might need, to support the entities that surround you, and you will realize the real power of the object.

Now, if you don't like the way the above example shows a date, and you want to have a realistic date in the same format, and you don't need to add formatdatetime () to each programdate attribute, you just embed the code into the attribute itself.

This requires a different method to define the attribute. Again, we will use programdate for externally visible properties, but because the Programdate property becomes a function instead of a static value, we 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

Objtvshow.starttime = CDate ("17:30")
Objtvshow.internal_programdate = DateSerial (1999,9,17)
Objtvshow.programtitle = "The Jerry Springer Show"


Response.Write Objtvshow.programtitle & "are on" & _
Objtvshow.starttime & "On" & Objtvshow.programdate & "."


The results of the procedure are as follows:

The Jerry Springer Show are on September 1999 5:30pm.





Thanks for Sophie's translation

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



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.