G
George
I might be being a little too tricky with Excel here but because there
doesn't seem to be support for overloading constructors with Class Modules in
Excel I've created a Factory class to instantiate objects and return them. I
have an "Employee" Class Module and a "Factory" Class Module and I'm trying
to get the factory to instantiate (and populate) an employee object based on
an id parameter.
Factory code:
Public Function getEmployee(id As Integer) As employee
Dim emp As employee
Dim staffSheet As Worksheet
Dim staffIDs As Range
Dim staffRow As Integer
Set staffSheet = Worksheets("Staff")
Set staffIDs = staffSheet.Range("A1",
staffSheet.Range("A65536").End(xlUp))
staffRow = staffIDs.Find(id).Row
Debug.Print staffRow
If staffRow < 1 Then
getEmployee = Null
Exit Function
End If
Set emp = New employee
getEmployee = emp
End Function
Yes - I know - at the moment it doesn't populate, but that's because I can't
even get this code to work properly
The method that calls this code is:
Set factory = New factory
Set employee = factory.getEmployee(1)
with both the "factory" and "employee" declared in the general sections.
I'm getting a runtime error 91 - which I know - on the "set employee" line
but I'm at a loss as to why this might be happening. The Debug.Print line in
the factory code correctly reports the row number of the employee(1) as "2".
Any ideas what I'm doing wrong here? I've tried both "employee=factory..."
and "set employee=factory..." with the same error!
doesn't seem to be support for overloading constructors with Class Modules in
Excel I've created a Factory class to instantiate objects and return them. I
have an "Employee" Class Module and a "Factory" Class Module and I'm trying
to get the factory to instantiate (and populate) an employee object based on
an id parameter.
Factory code:
Public Function getEmployee(id As Integer) As employee
Dim emp As employee
Dim staffSheet As Worksheet
Dim staffIDs As Range
Dim staffRow As Integer
Set staffSheet = Worksheets("Staff")
Set staffIDs = staffSheet.Range("A1",
staffSheet.Range("A65536").End(xlUp))
staffRow = staffIDs.Find(id).Row
Debug.Print staffRow
If staffRow < 1 Then
getEmployee = Null
Exit Function
End If
Set emp = New employee
getEmployee = emp
End Function
Yes - I know - at the moment it doesn't populate, but that's because I can't
even get this code to work properly
The method that calls this code is:
Set factory = New factory
Set employee = factory.getEmployee(1)
with both the "factory" and "employee" declared in the general sections.
I'm getting a runtime error 91 - which I know - on the "set employee" line
but I'm at a loss as to why this might be happening. The Debug.Print line in
the factory code correctly reports the row number of the employee(1) as "2".
Any ideas what I'm doing wrong here? I've tried both "employee=factory..."
and "set employee=factory..." with the same error!