Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign Objects from ListBox to ArrayList in a single statement?

In Delphi Prism, I need to assign objectcollection from ListBox to an ArrayList in a single statement. So far I have not found any solution.

In Delphi, this is how I did it.

theUser.Groups.Assign(ListBox1.Items);

Groups is a TList in Delphi and ArrayList in Delphi Prism. When I tried to do the same in delphi prism, it gives me the following errors.

"Groups.TGroupList" does not contain a definition for "Assign" in expression "theUser.groups.Assign"

If ArrayList doesn't have method that accepts objectcollection, then I will have to loop through each objects in the ListBox items and add it to ArrayList.

How would you do it?

Thanks in advance.

like image 761
ThN Avatar asked May 02 '26 14:05

ThN


2 Answers

Untested:

theUser.Groups.AddRange(ListBox1.Items)

ArrayList.AddRange accepts the ICollection interface which ListBox.ObjectCollection implements.

See also:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.aspx

http://msdn.microsoft.com/en-us/library/system.collections.arraylist.addrange(VS.71).aspx

like image 200
Jens Mühlenhoff Avatar answered May 05 '26 09:05

Jens Mühlenhoff


You should use the AddRange() method of ArrayList.

The equivalent to your Delphi code is:

theUser.Groups.Clear();
theUser.Groups.AddRange(ListBox1.Items);
like image 27
David Heffernan Avatar answered May 05 '26 09:05

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!