[Gambas-user] Re: Passing objects by reference
Christian Faure
christian.faurebouvard at ...357...
Thu Dec 16 00:10:01 CET 2004
El Miércoles, 15 de Diciembre de 2004 21:14, Chris Wilson escribió:
> Ok, I think I figured out the problem with the Array. I was trying to
> call the Add method of the array outside a statment(sub or
> function/method). Even when I get the Result in an Array however, I'm
> not getting it to pass as reference. The code should explain.
>
>
> db AS NEW Connection
> myRS AS Result
>
> myRSArray AS Object[]
> '''The second line gave me problems
> '''Seems as if you can't call methods outside statments
> '''just declare variables, right?
> 'myRSArray = NEW Object[]
> 'myRSArray.Add(myRS) '''Always get "missing AS" error
>
> PUBLIC SUB Form_Open()
> IF MakeConnection("localhost","user","password","test") THEN
> '''moved the above to here and I don't get the "missing AS"
> error anymore
> myRSArray = NEW Object[]
> myRSArray.Add(myRS) ''' works here
> '''I'm still not getting it to pass as reference,
> '''even when I get the result into an Array
> getData(myRSArray[0], "select * from student")
> TextBox1.Text = myRSArray[0]!id ''this gives me "NULL Object"
> Error TextBox2.Text = myRSArray[0]!name
> END IF
> END
>
> PUBLIC SUB getData(rs AS Result, qry AS String)
> rs = db.Exec(qry)
> PRINT rs!id & " - " & rs!name ''works here
> END
>
> PUBLIC FUNCTION MakeConnection(host AS String, lgn AS String, pass AS
> String, database AS String) AS Boolean
> db.close
> db.Type = "mysql"
> db.Name = database
> db.Host = host
> db.Login = lgn
> db.Password = pass
> db.Open
> RETURN TRUE
> CATCH
> Message.error(ERROR.text)
> RETURN FALSE
> END
>
> The mysql table has 2 columns "id" and "name"
>
> Thanks for the help
>
> Chris
>
Hi,
You need to pass the array, not the fisrt element.
Modify Form_Open and GetData like this:
PUBLIC SUB Form_Open()
IF MakeConnection("localhost","user","password","test") THEN
myRSArray = NEW Object[]
'not needed:
'myRSArray.Add(myRS) ''' works here
'replace myRSArray[0] by myRSArray
'to pass the array, not the first element of the array
getData(myRSArray, "select * from student")
TextBox1.Text = myRSArray[0]!id ''Now, this work
TextBox2.Text = myRSArray[0]!name
END IF
END
'this receive an array of objects
'then add an element: the resultset
PUBLIC SUB getData(MyArr AS Object[], qry AS String)
MyArr.Add( db.Exec(qry))
PRINT MyArr[0]!id & " - " & MyArr[0]!name ''works here
END
Regards,
Christian Faure
More information about the User
mailing list