Class Statement (VBScript)

Declares the name of a class, as well as a definition of the variables, properties, and methods that comprise the class.

Syntax

Class name
   statementsEnd Class 

Arguments

  • name
    Required. Name of the Class; follows standard variable naming conventions.

  • statements
    Required. One or more statements that define the variables, properties, and methods of the Class.

Remarks

Within a Class block, members are declared as either Private or Public using the appropriate declaration statements. Anything declared as Private is visible only within the Class block. Anything declared as Public is visible within the Class block, as well as by code outside the Class block. Anything not explicitly declared as either Private or Public is Public by default. Procedures (either Sub or Function) declared Public within the class block become methods of the class. Public variables serve as properties of the class, as do properties explicitly declared using Property Get, Property Let, and Property Set. Default properties and methods for the class are specified in their declarations using the Default keyword. See the individual declaration statement topics for information on how this keyword is used.

The following example illustrates the use of the Class statement.

Class Customer
   Private m_CustomerName
   Private m_OrderCount

   Private Sub Class_Initialize
      m_CustomerName = ""
      m_OrderCount = 0
   End Sub

   ' CustomerName property.
   Public Property Get CustomerName
      CustomerName = m_CustomerName
   End Property

   Public Property Let CustomerName(custname)
      m_CustomerName = custname
   End Property

   ' OrderCount property (read only).
   Public Property Get OrderCount
      OrderCount = m_OrderCount
   End Property

   ' Methods.
   Public Sub IncreaseOrders(valuetoincrease)
      m_OrderCount = m_OrderCount + valuetoincrease
   End Sub
End Class


Dim c
Set c = New Customer
c.CustomerName = "Fabrikam, Inc."
MsgBox (c.CustomerName)

c.IncreaseOrders(5)
c.IncreaseOrders(3)
MsgBox (c.OrderCount)

Requirements

Version 5

Change History

Date

History

Reason

March 2009

Added an example.

Customer feedback.

See Also

Dim Statement
Function Statement (VBScript)
Private Statement
Property Get Statement
Property Let Statement
Property Set Statement
Public Statement
Set Statement
Sub Statement