listbox as return value?

Can an object, like a listbox, be returned by a function?

for example:

reduceListBox (lst as listbox) as listbox

// receiving a listbox in lst
// doing some stuff

return theNewList

The compiler accepts this function, but when called I get an error:
“This method cannot accept an assigned value (it lacks an Assign parameter)”

the caller:
Listbox2 = enriMod.reduceListBox (Listbox1)

A similar method works with arrays (receiving and returning)

Whats ListBox2 defined as ?
Is it already a control on a layout ?

Yes, listbox1 and listbox2 are in the main window where is the button that call the functions.
The function is in a module.

Then no you cant replace and existing control on a window by doing this

You can adjust the contents of the one you have though - why not do that to the existing one ?

If I write the call like this, then the compiler accepts

dim lst as new Listbox

lst = enriMod.reduceListBox (Listbox1)

But I got a crash when I inspect the content of the lst either in the received or in the Function in the module


dim i, items, cont as integer
dim s, t as string

dim reduced_lst as new Listbox

reduced_lst.ColumnCount = 2

cont = 1
items = lst.listcount - 1

for i = 0 to items
s = lst.List(i)
if i+1 > items then
reduced_lst.AddRow lst.List(i) , str(cont)
t = lst.List(i+1)
if s = t then
cont = cont + 1
reduced_lst.AddRow lst.List(i) , str(cont)
Cont = 1
end if
End if
return reduced_lst

Yeah this wont work like you expect

Any suggestion?

Objects (such as Listboxes) are passed by reference anyways…

So if you have a listbox on your window (or for example TWO)

FUNCTION reduce_listbox(lb as listbox)

Do all your operations on LB, this being the listbox being passed, and it will update that instance
but creating a NEW listbox won't work the way you thought
[END function]

Thanks, I will try a workaround.

Yes, you can return a listbox (or any object), but you are using the listbox in an incorrect manner. The problem is not with your syntax, it’s with your usage.

Where can I find an example where a listbox is returned?
Or where is documented?, I has not been able to found in the Xojo documentation

You don’t want to return a listbox, there is nothing you can do with it. Instead edit the properties of the listbox directly in the method, or create a class which represents the data in the listbox and then populate the second listbox from that data after you have processed it.

Thanks Jason (and everyone that answered me), I understand I was in the bad way.
I have been here because precisely I tried to avoid to edit directly, from the module method, the original listbox, but I see it’s the way it should be done. Even if, in that case, I must change how the method do the job.

If you want to leave an unedited version you should definitely create a class to represent the data. That will also make it easier later on if you want to do anything special with the data (printing, saving, etc). Its up to you and your needs though.

One example of when you would want to return a listbox is where you have 2 similar listboxes and you want to choose between them using some complicated logic. You would put the logic in a method and return one of the listboxes.

dim lb as MyListboxClass
lb = SelectAListbox(somedata, someotherdata)
lb.Cell(0,0) = "Done."