But I got a crash when I inspect the content of the lst either in the received or in the Function in the module
FUNCTION
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)
else
t = lst.List(i+1)
if s = t then
cont = cont + 1
else
reduced_lst.AddRow lst.List(i) , str(cont)
Cont = 1
end if
End if
next
return reduced_lst
Objects (such as Listboxes) are passed by reference anyways…
So if you have a listbox on your window (or for example TWO)
reduce_listbox(listbox1)
reduce_listbox(listbox2)
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]
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.
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.DoSomething
lb.Cell(0,0) = "Done."