Results 1 to 4 of 4
  1. #1
    4 Star Lounger
    Join Date
    May 2004
    Location
    Toronto, Ontario, Canada
    Posts
    425
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Class Module help (Excel 2000)

    Hi all,

    I found this example on the net of a class module and need some help to explain why some of the code does not work.
    cStudent class module

    Option Explicit
    Public FirstName As String
    Public LastName As String
    Public Birthdate As Date
    ' In the Student class module.
    Public Sub Speak()
    ' A simple method.
    MsgBox "My name is " & Me.FirstName & " " & _
    Me.LastName & ". I was born on " & _
    Me.Birthdate & "."
    End Sub

    in a normal module
    Option Explicit

    Sub TestStudent1()
    Dim oStudent As cStudent
    ' Make the object variable point to a real object.
    Set oStudent = New cStudent
    ' Set properties of the new object.
    With oStudent
    .FirstName = "Sally"
    .LastName = "Smith"
    .Birthdate = #5/1/1990#
    End With
    ' Retrieve some of the properties.
    Debug.Print oStudent.FirstName & " " & _
    oStudent.LastName & oStudent.Birthdate
    ' Call a method of the object.
    oStudent.Speak
    ' Destroy the object.
    Set oStudent = Nothing
    End Sub

    Sub TestStudent3()
    Dim oStudent1 As cStudent
    Dim oStudent2 As cStudent
    Set oStudent2 = New cStudent
    With oStudent1
    .FirstName = "Bart"
    .LastName = "Simmons"
    .Birthdate = #6/6/1987#
    End With
    With oStudent2
    .FirstName = "Lisa"
    .LastName = "Simmons"
    .Birthdate = #5/1/1991#
    End With
    oStudent1.Speak
    oStudent2.Speak
    End Sub

    Public Sub TEST4Student()
    Dim oNewStudent As cStudent
    Set oNewStudent = New cStudent
    With oNewStudent
    .FirstName = "Bart"
    .LastName = "Simmons"
    .Birthdate = #6/6/1987#
    End With
    cStudents.Add oNewStudent, oNewStudent.FirstName
    With oNewStudent
    .FirstName = "Lisa"
    .LastName = "Simmons"
    .Birthdate = #5/1/1991#
    End With
    Students.Add oNewStudent, oNewStudent.FirstName
    End Sub

    I can get Sub TestStudent1() to work, but I can't get the other 2 examples to work, I thought I would post it here and ask why?

    I keep getting object variable not set error, if some one has the time could the tell me why they are not set.

    Thanks,
    Darryl.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Class Module help (Excel 2000)

    The second example (TestStudent3) lacks one line:

    Set oStudent1 = New cStudent

    The last example (TEST4Student) uses a Collection but it hasn't been declared:

    Dim Students As New Collection

    and it suffers from inconsistent use of variable names: cStudents should be Students. This example adds two items to the collection, but it doesn't provide any output.

  3. #3
    4 Star Lounger
    Join Date
    May 2004
    Location
    Toronto, Ontario, Canada
    Posts
    425
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Re: Class Module help (Excel 2000)

    Thanks Hans,

    I just need to ask with the declaration of the collection, I am still accessing the cStudent Class? And with output you mean exposing the data?

    Darryl.

    Public Sub TEST4Student()
    Dim Students As New Collection
    Dim oNewStudent As cStudent
    Set oNewStudent = New cStudent
    With oNewStudent
    .FirstName = "Bart"
    .LastName = "Simmons"
    .Birthdate = #6/6/1987#
    End With
    Students.Add oNewStudent, oNewStudent.FirstName
    Debug.Print oNewStudent.FirstName
    With oNewStudent
    .FirstName = "Lisa"
    .LastName = "Simmons"
    .Birthdate = #5/1/1991#
    End With
    Students.Add oNewStudent, oNewStudent.FirstName
    Debug.Print oNewStudent.FirstName
    End Sub

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Class Module help (Excel 2000)

    This procedure defines a Collection object, and adds two instances of the cStudent class to it: you call Students.Add twice. You added Debug.Print lines that were not in the original, so the procedure now produces some output. But the code still doesn't do anything with the collection (not that you have to, of course <img src=/S/grin.gif border=0 alt=grin width=15 height=15>)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •