Return recordset from a class.

T

terje

I have this code:

Class MyClass

Private Sub Class_Initialize()
If IsEmpty(MyRecordset) Then Set MyRecordset =
Server.CreateObject("ADODB.Recordset")
End Sub

Public Function Foo
Set Foo = MyRecordset
End Function

End Class

And I call this class with code like this, using the Set keyword:

With cMyClass
Set oRs = .Foo
End With

What is going on here? Am I creating a second recordset (a copy), or am
I just passing a reference/pointer to the recordset created in he class?
Also, I have noticed that when I leave out the Set keyword, a fields
collection is returned, not a recordset. Interesting.

terje
 
B

Bob Barrows [MVP]

terje said:
I have this code:

Class MyClass

Private Sub Class_Initialize()
If IsEmpty(MyRecordset) Then Set MyRecordset =
Server.CreateObject("ADODB.Recordset")
End Sub

Public Function Foo
Set Foo = MyRecordset
End Function

End Class

And I call this class with code like this, using the Set keyword:

With cMyClass
Set oRs = .Foo
End With

You left out the global declaration for the MyRecordset object in your class
so the Foo function really should not really be returning a recordset, but
that's neither here nor there.
What is going on here? Am I creating a second recordset (a copy), or
am I just passing a reference/pointer to the recordset created in he
class?

All objects are passed by reference (unless they go across process
boundaries). That means that you are indeed passing a pointer to the
original recordset.

Also, I have noticed that when I leave out the Set keyword, a
fields collection is returned, not a recordset. Interesting.

Fields is the default property, so without the Set keyword, the default
property is assigned to the variable, not the object.

Bob Barrows
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top