Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 347
Default Error when returning user-defined class from "factory" class...

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!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Error when returning user-defined class from "factory" class...


"George" wrote in message
...


If staffRow < 1 Then
getEmployee = Null
Exit Function
End If

Set emp = New employee
getEmployee = emp
End Function


You need to use the Set keyword /every/ time you assign an object (including
a return value of a function):

If staffRow < 1 Then
set getEmployee = Nothing ' Note that 'Nothing' is a null object
in VB
Exit Function
End If

Set emp = New employee
Set getEmployee = emp

Rob

  #3   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Error when returning user-defined class from "factory" class...

I think you need set statements within your getemployee function since your
function returns an object.

Set getEmployee = Null
Set getEmployee = emp


"George" wrote:

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!

  #4   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Error when returning user-defined class from "factory" class..

Also, I'm not positive about this:
Set getEmployee = Null

Usually, I would use:
Set getEmployee = Nothing



"JMB" wrote:

I think you need set statements within your getemployee function since your
function returns an object.

Set getEmployee = Null
Set getEmployee = emp


"George" wrote:

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!

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 347
Default Error when returning user-defined class from "factory" class..

Jim - thanks for the response. You're quite right about the "Nothing" - the
"Null" is a hangup from my experience with other languages :-)

I've gone the whole hog with your suggestion as well and tried:

Factory.getEmployee :-
Set getEmployee = Nothing
Set emp = Nothing
Set emp = New Employee
Set getEmployee = emp

As well as setting "employee" to nothing before the call to getEmployee
however I'm still getting the runtime 91 error.

Any other ideas about what I'm doing wrong - I'm still thinking it's my code
rather than anything else!

Interestingly I get the same error if I try to find an ID that I know
doesn't exist. Not sure if that helps, but it might.
Thanks

"JMB" wrote:

I think you need set statements within your getemployee function since your
function returns an object.

Set getEmployee = Null
Set getEmployee = emp


"George" wrote:

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!



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 347
Default Error when returning user-defined class from "factory" class..

And now I have it working - except when there's no employee to return, but I
can work that out. I could have sworn I tried all the combinations but putting

I think in all my crazy late night editing last night I removed the "Set"
statement from the caller method - now that's back in your suggestion is just
the ticket Jim!

Thanks for the assistance!

George


Jim - thanks for the response. You're quite right about the "Nothing" - the
"Null" is a hangup from my experience with other languages :-)

I've gone the whole hog with your suggestion as well and tried:

Factory.getEmployee :-
Set getEmployee = Nothing
Set emp = Nothing
Set emp = New Employee
Set getEmployee = emp

As well as setting "employee" to nothing before the call to getEmployee
however I'm still getting the runtime 91 error.

Any other ideas about what I'm doing wrong - I'm still thinking it's my code
rather than anything else!

Interestingly I get the same error if I try to find an ID that I know
doesn't exist. Not sure if that helps, but it might.
Thanks

"JMB" wrote:

I think you need set statements within your getemployee function since your
function returns an object.

Set getEmployee = Null
Set getEmployee = emp



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Unable to get Pivot Tables Property of Worksheet Class "Error [email protected] Excel Programming 2 April 30th 07 06:18 PM
Hiding Rows Error - "Runtime Error 1004 - Unable to set the hidden property of the range class" Punsterr Excel Programming 2 April 9th 07 05:32 PM
Error Message "Select method of worksheet class failed" philc Excel Programming 5 May 4th 05 08:07 AM
VBA error 1004 "Select method of Range class failed" Matt J Excel Programming 6 July 3rd 04 10:05 PM
Error 1004, "select method of range class failed" paritoshmehta[_11_] Excel Programming 3 May 6th 04 10:09 PM


All times are GMT +1. The time now is 11:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"