Creating and Using a Class Module : Class « Data Type Functions « VBA / Excel / Access / Word

Home
VBA / Excel / Access / Word
1.Access
2.Application
3.Data Type
4.Data Type Functions
5.Date Functions
6.Excel
7.File Path
8.Forms
9.Language Basics
10.Math Functions
11.Outlook
12.PowerPoint
13.String Functions
14.Windows API
15.Word
16.XML
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
VBA / Excel / Access / Word » Data Type Functions » Class 
Creating and Using a Class Module
 

'Class Module: Employee
Public FirstName As String
Public LastName As String
Public Function Speak()
     Speak = FirstName & " " & LastName
End Function
'test Module
Sub SingleInstance()
    Dim oPerson As Employee
    Set oPerson = New Employee

    oPerson.FirstName = "A"
    oPerson.LastName = "Balter"

    msgBox oPerson.Speak
End Sub

''Property Let and Get Adding Properties the Right Way
Private mstrFirstName As String
Private mstrLastName As String
Public Property Get FirstName() As String
    FirstName = mstrFirstName
End Property

Public Property Let FirstName(ByVal strNewValue As String)
    mstrFirstName = UCase(strNewValue)
End Property

Public Property Get LastName() As String
    LastName = mstrLastName
End Property

Public Property Let LastName(ByVal strNewValue As String)
    mstrLastName = UCase(strNewValue)
End Property

'Setting Values with Property Set
Private mobjCustomer As Employee

Public Property Set GoodEmployee(objCustomer As Employee)
     Set mobjCustomer = objCustomer
End Property
'Class Module: Employee
Public FirstName As String
Public LastName As String
Public Function Speak()
     Speak = FirstName & " " & LastName
End Function
'test Module
Sub SingleInstance()
    Dim oPerson As Employee
    Set oPerson = New Employee

    oPerson.FirstName = "A"
    oPerson.LastName = "Balter"

    msgBox oPerson.Speak
End Sub
'Creating Multiple Class Instances
Sub MultipleInstance()
    Dim oPerson1 As Employee
    Dim oPerson2 As Employee

    Set oPerson1 = New Employee
    Set oPerson2 = New Employee

    oPerson1.FirstName = "A"
    oPerson1.LastName = "B"

    Debug.Print oPerson1.Speak

    oPerson2.FirstName = "Dan"
    oPerson2.LastName = "B"

    Debug.Print oPerson2.Speak

End Sub
'Define Class Initialize event method and Terminate event method
Public FirstName As String
Public LastName As String
Public Function Speak()
     Speak = FirstName & " " & LastName
End Function


'test Module
Sub SingleInstance()
    Dim oPerson As Employee
    Set oPerson = New Employee

    oPerson.FirstName = "A"
    oPerson.LastName = "Balter"

    msgBox oPerson.Speak
End Sub

'Use the Initialize event to perform tasks such as establishing a connection to a database and initializing variables.
Private Sub Class_Initialize()
    FirstName = "A"
    LastName = "Balter"
End Sub
'You generally use the Terminate event to perform the class's cleanup tasks.
Private Sub Class_Terminate()
    rstCustomer.Close
    Set rstCustomer = Nothing
End Sub

 
Related examples in the same category
1.Creating Custom Objects
2.Class Module: FileInformation
3.Class Module: FullName
4.The SimpleLoan Class
5.Loan Object Implementation Details
6.Rectangle class
7.Creating the MyRectangle Class
8.Abstraction
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.