Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Parameters

Status
Not open for further replies.

MDTekUser

Technical User
Dec 25, 2005
68
US
I am passing a recordset through tiers in an application. Is it better to pass By Reference or By Value? I am not going to be modifying the recordset in any way after it's passed.

I know that By Reference is used when you need to modify the object being passed. When I use By Reference am I creating a copy of the recordset because I am using a pointer to where the recordset resides in memory?

Another question. If I pass a form object in a parameter, am I creating another instance of it?
 
As far as I know, if you pass an Object byval, it actually passes by ref anyway.

Here's an example to show what I mean....

Code:
Private Sub Command1_Click()
    
    Dim RS As ADODB.Recordset
    
    Set RS = CreateObject("ADODB.Recordset")
    
    Call RS.Fields.Append("SomeNum", adInteger)
    Call RS.Open
    
    MsgBox RS.RecordCount
    Call ByValTest(RS)
    MsgBox RS.RecordCount
    
    Call ByRefTest(RS)
    MsgBox RS.RecordCount
    RS.Close
    Set RS = Nothing
    
End Sub

Private Sub ByValTest(ByVal RS As ADODB.Recordset)
    
    RS.AddNew
    RS("SomeNum").Value = 1
    RS.Update
    
End Sub

Private Sub ByRefTest(ByRef RS As ADODB.Recordset)

    RS.AddNew
    RS("SomeNum").Value = 1
    RS.Update


End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I have to check on that one because I would think there would be a difference. But you are right, you are able to change the values in a recordset even if you pass by value.
 
If you pass byval, you're actually passing the value of the object reference. Meaning, you're passing the object reference.

HTH

Bob
 
Well, on further study I found out when I pass a parameter by value I am creating a copy of the object. That is supposed to explain why you can't modify the object passed by value.
 
Here's some code. First, a class module:
Code:
Option Explicit

Public myProperty As Integer

Then, in a form module:
Code:
Option Explicit

Private Sub Form_Load()
Dim q As New Class1
q.myProperty = 3
Debug.Print q.myProperty
Test q
Debug.Print q.myProperty
Test2 q
Debug.Print q.myProperty
End Sub

Public Sub Test(ByVal x As Class1)
x.myProperty = 17
End Sub

Public Sub Test2(ByRef x As Class1)
x.myProperty = 23
End Sub

The debug output is
3
17
23

Suggesting that you CAN modify the object passed by value?

Bob
 
Bob,

Those results are the same as my ADO Recordset example.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
<Those results are the same as my ADO Recordset example.

Which is to be expected. :) My code was in response to MDTekUser's post.

Again, there isn't any difference between the value of the object reference and the reference itself, which is why as George says when you pass an object byval, you're actually passing it byref anyway.

MDTekUser, if your further study contradicts this, please post links to the reference material, as I would like to read it.

Thanks,

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top