byval and object instance

Discussion in 'ASP General' started by WG@., Aug 20, 2005.

  1. WG@.

    WG@. Guest

    Here is an ASP code sample in VBScript:

    <%
    Dim x
    Set x = server.CreateObject("ADODB.RECORDSET")
    x.CursorType = 1
    x.CursorLocation = 3
    x.Fields.Append "custtype", adVarNumeric, 80, adFldMayBeNull
    x.Open
    x.addnew "custtype", 34
    x.Update

    call dothis(x)

    Response.Write(x.RecordCount)

    Set x = nothing

    Function dothis(byval x)

    x.addnew "custtype", 34
    Response.Write(x.RecordCount & "<BR>")

    x.addnew "custtype", 34
    Response.Write(x.RecordCount & "<BR>")

    set x = nothing

    End Function

    %>

    This should run if you put it into an asp page and pull it from IIS.
    It is just a disconnected recordset filled with a record and then a
    response.write, etc.

    Question: I'm using "byval" in the dothis() function, but SHOULD I
    set x = nothing within the function? Obviously when using, "byref",
    setting x = nothing will delete the instance of the object, but with
    "byval" will I leave something in memory if I don't set x = nothing?

    I think this is another "age old" question but I've got some pages
    acting funky on a hosted server in Canada and I think this issue may
    have something to do with it.
    WG@., Aug 20, 2005
    #1
    1. Advertising

  2. WG@. wrote:
    > Here is an ASP code sample in VBScript:
    >
    > <%
    > Dim x
    > Set x = server.CreateObject("ADODB.RECORDSET")
    > x.CursorType = 1
    > x.CursorLocation = 3
    > x.Fields.Append "custtype", adVarNumeric, 80, adFldMayBeNull
    > x.Open
    > x.addnew "custtype", 34
    > x.Update
    >
    > call dothis(x)
    >
    > Response.Write(x.RecordCount)
    >
    > Set x = nothing
    >
    > Function dothis(byval x)
    >
    > x.addnew "custtype", 34
    > Response.Write(x.RecordCount & "<BR>")
    >
    > x.addnew "custtype", 34
    > Response.Write(x.RecordCount & "<BR>")
    >
    > set x = nothing
    >
    > End Function
    >
    > %>
    >
    > This should run if you put it into an asp page and pull it from IIS.
    > It is just a disconnected recordset


    <pet peeve ignorable="true">
    Actually, it's an "ad hoc" recordset. "Disconnected" implies that the
    recordset was once connected to a database and disconnected after it was
    opened. "Disconnected" recordsets can be reconnected to the database from
    which it was disconnected. "Ad hoc" recordsets can never be connected to a
    database.
    </pet peeve>

    > filled with a record and then a
    > response.write, etc.
    >
    > Question: I'm using "byval" in the dothis() function, but SHOULD I
    > set x = nothing within the function? Obviously when using, "byref",
    > setting x = nothing will delete the instance of the object, but with
    > "byval" will I leave something in memory if I don't set x = nothing?
    >


    Objects are always passed ByRef, even when you specify ByVal. You can prove
    this by the following code:
    <%
    test

    sub test
    dim rs, fld
    set rs=CreateObject("adodb.recordset")
    rs.Fields.append "Field1",adVarChar,20
    rs.Open
    rs.AddNew array("Field1"),array("Test")
    Response.Write "Before calling sub:<BR>"
    Response.Write rs.GetString(adClipString,,"; ","<BR>","null")
    AddFieldAndRecord rs
    rs.MoveFirst
    Response.Write "<BR>After calling sub:<BR>"
    Response.Write rs.GetString(adClipString,,"; ","<BR>","null")
    rs.Close:set rs=nothing
    end sub

    sub AddFieldAndRecord(ByVal pRs)
    prs.AddNew array("Field1"),array("Test2")
    prs.MoveFirst
    Response.Write "<BR>Inside sub:<BR>"
    Response.Write prs.GetString(adClipString,,"; ","<BR>","null")
    end sub

    %>

    > I think this is another "age old" question but I've got some pages
    > acting funky on a hosted server in Canada and I think this issue may
    > have something to do with it.


    Since there is no "parent" object (such as a connection), this recordset
    object should be successfully cleaned up by the garbage collector (GC).
    However, you should explicitly close the recordset when finished with it,
    just to be sure that the GC can successfully destroy it. Explicitly setting
    a recordset to Nothing should cause it to be closed, but there is no harm in
    making sure ...

    More info about garbage collection can be found at:
    http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx

    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], Aug 20, 2005
    #2
    1. Advertising

  3. WG@.

    WG@. Guest

    On Sat, 20 Aug 2005 11:25:56 -0400, "Bob Barrows [MVP]"
    <> wrote:

    >WG@. wrote:
    >> Here is an ASP code sample in VBScript:
    >>
    >> <%
    >> Dim x
    >> Set x = server.CreateObject("ADODB.RECORDSET")
    >> x.CursorType = 1
    >> x.CursorLocation = 3
    >> x.Fields.Append "custtype", adVarNumeric, 80, adFldMayBeNull
    >> x.Open
    >> x.addnew "custtype", 34
    >> x.Update
    >>
    >> call dothis(x)
    >>
    >> Response.Write(x.RecordCount)
    >>
    >> Set x = nothing
    >>
    >> Function dothis(byval x)
    >>
    >> x.addnew "custtype", 34
    >> Response.Write(x.RecordCount & "<BR>")
    >>
    >> x.addnew "custtype", 34
    >> Response.Write(x.RecordCount & "<BR>")
    >>
    >> set x = nothing
    >>
    >> End Function
    >>
    >> %>
    >>
    >> This should run if you put it into an asp page and pull it from IIS.
    >> It is just a disconnected recordset

    >
    ><pet peeve ignorable="true">
    >Actually, it's an "ad hoc" recordset. "Disconnected" implies that the
    >recordset was once connected to a database and disconnected after it was
    >opened. "Disconnected" recordsets can be reconnected to the database from
    >which it was disconnected. "Ad hoc" recordsets can never be connected to a
    >database.
    ></pet peeve>
    >
    >> filled with a record and then a
    >> response.write, etc.
    >>
    >> Question: I'm using "byval" in the dothis() function, but SHOULD I
    >> set x = nothing within the function? Obviously when using, "byref",
    >> setting x = nothing will delete the instance of the object, but with
    >> "byval" will I leave something in memory if I don't set x = nothing?
    >>

    >
    >Objects are always passed ByRef, even when you specify ByVal. You can prove
    >this by the following code:
    ><%
    >test
    >
    >sub test
    > dim rs, fld
    > set rs=CreateObject("adodb.recordset")
    > rs.Fields.append "Field1",adVarChar,20
    > rs.Open
    > rs.AddNew array("Field1"),array("Test")
    > Response.Write "Before calling sub:<BR>"
    > Response.Write rs.GetString(adClipString,,"; ","<BR>","null")
    > AddFieldAndRecord rs
    > rs.MoveFirst
    > Response.Write "<BR>After calling sub:<BR>"
    > Response.Write rs.GetString(adClipString,,"; ","<BR>","null")
    > rs.Close:set rs=nothing
    >end sub
    >
    >sub AddFieldAndRecord(ByVal pRs)
    > prs.AddNew array("Field1"),array("Test2")
    > prs.MoveFirst
    > Response.Write "<BR>Inside sub:<BR>"
    > Response.Write prs.GetString(adClipString,,"; ","<BR>","null")
    >end sub
    >
    >%>


    Change your sub to the sub below and it produces an error "Object
    Required" in sub test because the instance of the object created has
    been destroyed; then change ByRef to ByVal. The error goes away;
    setting pRs = nothing has no effect on the instance of the object in
    memory. When using ByVal, I believe all that gets passed is the
    "cursor", but not a "copy" of the object (basically just like ByRef).
    Notice the var type of pRs when using ByVal is a "9" "Automation
    Object" (which makes sense anyway, I guess). COM is paying attention
    to a difference between byval and byref.

    Is it neccessary, or a "good idea" to set the locally dimensioned pRs
    to nothing when using ByVal? Or, does the variable (pointer) pRs just
    go out of scope?


    sub AddFieldAndRecord(ByRef pRs)
    prs.AddNew array("Field1"),array("Test2")
    prs.MoveFirst
    Response.Write "<BR>Inside sub:<BR>"
    Response.Write prs.GetString(adClipString,,"; ","<BR>","null")

    Response.Write "<BR>pRs vartype:<BR>"
    Response.Write(varType(pRs))

    set pRs = nothing

    end sub


    >
    >> I think this is another "age old" question but I've got some pages
    >> acting funky on a hosted server in Canada and I think this issue may
    >> have something to do with it.

    >
    >Since there is no "parent" object (such as a connection), this recordset
    >object should be successfully cleaned up by the garbage collector (GC).
    >However, you should explicitly close the recordset when finished with it,
    >just to be sure that the GC can successfully destroy it. Explicitly setting
    >a recordset to Nothing should cause it to be closed, but there is no harm in
    >making sure ...
    >
    >More info about garbage collection can be found at:
    >http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx
    >
    >Bob Barrows
    WG@., Aug 20, 2005
    #3
  4. WG@. wrote:
    >
    > Change your sub to the sub below and it produces an error "Object
    > Required" in sub test because the instance of the object created has
    > been destroyed; then change ByRef to ByVal. The error goes away;
    > setting pRs = nothing has no effect on the instance of the object in
    > memory.


    This may help.
    http://blogs.msdn.com/ericlippert/archive/0001/01/01/53005.aspx
    If not, I'm out of my depth. You may wish to email Eric (he's on vacation at
    the moment


    > When using ByVal, I believe all that gets passed is the
    > "cursor", but not a "copy" of the object (basically just like ByRef).


    This does not sound correct, but again, I'm out of my depth ...

    > Notice the var type of pRs when using ByVal is a "9" "Automation
    > Object" (which makes sense anyway, I guess). COM is paying attention
    > to a difference between byval and byref.


    I don't understand the point you are making here. The vartype is 9
    regardless of whether byval or byref is used ...

    >
    > Is it neccessary, or a "good idea" to set the locally dimensioned pRs
    > to nothing when using ByVal? Or, does the variable (pointer) pRs just
    > go out of scope?


    Again, I believe it is not necessary given the lack of a parent object.



    --
    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], Aug 20, 2005
    #4
    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. David Waz...
    Replies:
    0
    Views:
    407
    David Waz...
    Jul 4, 2003
  2. Kathy Burke
    Replies:
    2
    Views:
    5,143
    Kevin Spencer
    Aug 1, 2003
  3. Showjumper
    Replies:
    2
    Views:
    1,805
    Showjumper
    Aug 2, 2003
  4. Andreas Klemt
    Replies:
    1
    Views:
    129
    Tim Pacl
    Jul 18, 2003
  5. David Lozzi
    Replies:
    2
    Views:
    352
    Harolds
    Jul 22, 2005
Loading...

Share This Page