Android Question Sending a Resultset to a sub, Changes are local to that sub, (behaviour changed from B4A v 9.90)

OMS

Member
Hi,
I have noticed that if I send a result set to a sub, any chages to that Result set is temporary in that sub.
It was normally behaving in B4A 9.80
And many Errors in my application riased since I updated to newer versions:

B4X:
Sub Process_Globals
     Dim SQL1 As SQL
    Dim myCur As ResultSet
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    File.Copy(File.DirAssets,"myData.db",File.DirInternal,"myData.db")
    SQL1.Initialize(File.DirInternal,"myData.db",False)
    myCur = SQL1.ExecQuery("SELECT * from History")
    Log("1 ColumnName(0) = " & myCur.GetColumnName(0))
    ChangeCursor(myCur)
    Log("2 ColumnName(0) = " & myCur.GetColumnName(0))
End Sub

Sub ChangeCursor(TempCur As ResultSet)
    TempCur = SQL1.ExecQuery("SELECT * from Stock")
    Log("3 ColumnName(0) = " & TempCur.GetColumnName(0))
End Sub

logs when runing with B4A version 9.80 :
B4X:
** Activity (main) Resume **
1 ColumnName(0) = LastVersion
3 ColumnName(0) = BarcodeValue
2 ColumnName(0) = BarcodeValue

and when compiled with B4A version 9.90 ... ver 11.0
B4X:
** Activity (main) Resume **
1 ColumnName(0) = LastVersion
3 ColumnName(0) = BarcodeValue
2 ColumnName(0) = LastVersion
 

Attachments

  • Sending Cursor to Sub.zip
    4.6 KB · Views: 114

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are correct. Your previous code relied on a bug in B4X. This bug was fixed. Note that the bug only happened in some cases (related to wrapper objects).

Assigning a value to a local variable should never have any effect on any other variables.
Correct code:
B4X:
Sub ChangeCursor As ResultSet
    Dim rs As ResultSet = SQL1.ExecQuery("SELECT * from Stock")
    Log("3 ColumnName(0) = " & TempCur.GetColumnName(0))
    Return rs
End Sub

myCur = ChangeCursor
 
Upvote 0

OMS

Member
Thanks Erel,
But I used to think that passing an object to a sub is always by reference.
I learned it from your comments here:


Anyway, Is there a way to pass values by reference to subs, in recent versions of B4A ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But I used to think that passing an object to a sub is always by reference.
Not exactly. A pointer is passed by value. This means that the object is not copied but it also means that assigning a different value to the parameter variable should never change anything else.

Anyway, Is there a way to pass values by reference to subs, in recent versions of B4A ?
It was never possible to pass by reference in B4A. There was one specific case where it happened due to a bug.

The correct code is the code I posted above.
There are ways to pass by reference using an array or custom type but you will only make things more complicated than they should be.
 
Upvote 0
Top