I’m creating a Method which allows me to save the entire contents of a list box into a SQLite field.
Here’s some sample code that grabs the ListBox Data and then loads it back again.
[code]headings = listbox1.Heading(-1) // puts headings into string
tableData = listbox1.Cell(-1, -1) // puts data into string
listbox1.HasHeading = True // listbox setup
listbox1.ColumnCount = Num // dynamic value
listbox1.heading(-1) = headings // loads string into listbox headings
listbox1.Cell(-1, -1) = tableData // loads string into listbox data[/code]
So here’s the problem. I want to make put something like this into a Method, where I pass in the ListBox name in some kind of variable, and then instead of referencing the list box with the literal “listbox1” using the variable, which contains the listbox name. Something like this.
tableData = passedTableName.Cell(-1, -1) // puts data into string
passedTableName.Cell(-1, -1) = tableData // loads string into listbox
You can pass the listbox object safely, but attempting to find a listbox based on a passed string is inefficient and opens yourself up to a load of bugs and vulnerabilities.
Public Method DoSomething(Extends lbTarget as Listbox, sData as String)
lbTarget.Cell(-1, -1) = sData
End Function
Function Contains(Extends Container As String, Contained As String) As Boolean
Return Container.InStr(Contained) > 0
End Function
And this code shows how it is used:
Dim s1 As String = "hello world"
Dim s2 As String = "world"
If s1.Contains(s2) Then
...
End If
Just trying out this simple example, I’m getting an error. I paste the sample function code into an empty action Button and a Method is nicely created. But then when I go to run, I get the error message, “The Extends modifier cannot be used in a Class or Method”. Then where?
Odd. I found quite a few Extends examples. They’re all functions, but Xojo seem to turn all functions into Methods. And the big blue Insert button (upper left) in the Xojo IDE doesn’t list function as a addable option.
[quote=437751:@Norman Palardy]A function is just a method that has a return type that returns a value
A sub is one without a return type
Both are methods[/quote]
that is interesting. i never knew that there was a difference under the hood (listed as sub vs function). this will make something I am doing a little more complicated.
[quote]A function is just a method that has a return type that returns a value.
A sub is one without a return type. Both are methods.[/quote]
Good point Norman! I just ran a test. Created a Method without a Return value and it worked fine. I had wondered how to create a simple sub-routine. So… Sub-routines are created as a Method; just without a Return value.
Back to Extends. Yes. Tim. That was it. All Extends definitions need to be in a Module. You also said…
From what I understand now, when using Extends, the listBox name gets passed fine. But operations on the listbox itself probably requires more than a name. (The window where the listBox lives also needs to be part of the reference.) Another Extends? Maybe thats more complicated than it needs to be.
At this point Im thinking the easiest solution would be to pass the entire Listbox by reference. That would include all the Listbox properties and methods, without any significant overhead, since a listbox passed by reference is not a copy, but rather a memory reference which gives the receiving Method full read write access to the entire actual lisbox straight away.
I’m not sure why Extends was brought into this. It isn’t necessary to accomplish what you want. It’s just another way to accomplish the same thing and it gives you a different way to call the method.
With Extends:
Listbox1.DoSomething
With a simple subroutine (which can be defined anywhere, it doesn’t have to be in a Module)
[quote=437988:@Norman Palardy]Anything that is not a simple type like integer string etc IS a reference type which is NOT the same as pass by reference
see
You can actually pass a reference type byref :)[/quote]
Thanks for the correction.