Passing a recordset into a method needs ByRef? -> not an object?

Hi all,

I always though of RecordSet as an object, but if I pass one into a method then I need to use ByRef.

Is a RecordSet not an object? I always thought objects are passed ByRef anyway?

TiA

Markus

I’ve never had to pass one by ref
Whats the code IN the method your passing it to look like ?

I pass RecordSets by value all the time.

ByRef means that the original variable of the calling method can be reassigned. When it comes to objects, you can change the values within the object either way.

This is the calling code:

[code]Dim data As RecordSet
Dim NumberOfProteins as Integer

sql = “SELECT * FROM tblProtein”

GetCountFromSQL( sql, Db, data, sr, st )

NumberOfProteins = data.RecordCount[/code]

and this is the method:

[code]Sub GetCountFromSQL(sql as String, Db as Database, data as RecordSet, sr as StyleRun, st as StyledText)

data = Db.SQLSelect( sql )

If Db.Error Then
sr.Text = "DB Error: " + Db.ErrorMessage

else

If data <> Nil Then
sr.Text = Str( data.RecordCount ) + EndOfLine
else
sr.Text = “” + EndOfLine
End If

end if

End Sub[/code]

I get a NoE on NumberOfProteins = data.RecordCount and data is Nil.

If I pass data with ByRef then it works.

So where am I going wrong?

Yes in this case you ARE required to pass by reference because you change the recordset ( see where you assign to it )

[quote=130981:@Markus Winter]This is the calling code:

[code]Dim data As RecordSet
Dim NumberOfProteins as Integer

sql = “SELECT * FROM tblProtein”

GetCountFromSQL( sql, Db, data, sr, st )

NumberOfProteins = data.RecordCount[/code]

and this is the method:

[code]Sub GetCountFromSQL(sql as String, Db as Database, data as RecordSet, sr as StyleRun, st as StyledText)

data = Db.SQLSelect( sql ) // <<<<<<<<<<<<<<<<<<<< this requires BYREF

[/quote]

But isn’t a RecordSet an object anyway and should be passed ByRef by default?

Perhaps this will illustrate. Suppose you had MyMethod(o as Object) (“object” here can mean any object.)

Sub MyMethod(o As Object)
  o.Prop = "value1"

  o = New Object
  o.Prop = "value2"
End Sub

The calling code looks like this:

dim o as new Object
MyMethod(o)
// o.Prop is now "value1"

Let’s change MyMethod to use ByRef:

Sub MyMethod(ByRef o As Object)
  o.Prop = "value1"

  o = New Object
  o.Prop = "value2"
End Sub

And the calling code:

dim o as new Object
MyMethod(o)
// o.Prop is now "value2"

In the first case, o still retains the new Object that was assigned directly above the call to MyMethod. In the second case, because of ByRef, o was assigned a new Object within MyMethod.

That’s the difference that ByRef makes.

You change what DATA is going to refer to - that requires BYREF

Ah, got it now.

Db.SQLSelect( sql ) returns an object, so data = Db.SQLSelect( sql ) re-assigns the pointer to this new object.

However NumberOfProteins = data.RecordCount still uses the original object which is still Nil.

Thanks Kem & Norman! And Matt! (one book I’ll never sell: REALbasic - the definitive guide, 2nd Ed)

Matts book still has many useful sections in it
Even after all these years