Return recordset from a class.

Discussion in 'ASP General' started by terje, Jun 24, 2005.

  1. terje

    terje Guest

    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
    terje, Jun 24, 2005
    #1
    1. Advertising

  2. terje wrote:
    > 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
    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
    Bob Barrows [MVP], Jun 24, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greenhorn
    Replies:
    15
    Views:
    797
    Keith Thompson
    Mar 6, 2005
  2. Geoff Robinson

    What Should You Do If You Want to Return a Recordset?

    Geoff Robinson, May 11, 2004, in forum: ASP .Net Web Services
    Replies:
    4
    Views:
    107
    Ahsan Yar Khan
    May 17, 2004
  3. Hung Huynh
    Replies:
    8
    Views:
    289
    Bob Barrows
    Sep 24, 2003
  4. Serge Myrand

    ADODB.RecordSet, RecordCount always return -1

    Serge Myrand, Jun 23, 2004, in forum: ASP General
    Replies:
    7
    Views:
    281
    Bob Barrows [MVP]
    Jun 30, 2004
  5. shank

    return highest value in recordset

    shank, Jun 25, 2004, in forum: ASP General
    Replies:
    13
    Views:
    224
    Dave Anderson
    Jun 26, 2004
Loading...

Share This Page