6 Replies - 10166 Views - Last Post: 29 March 2013 - 06:22 PM

#1 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1719
  • View blog
  • Posts: 5,636
  • Joined: 25-September 09

ObjectCollection vs SelectedObjectCollection

Posted 28 March 2013 - 06:51 PM

I stumbled upon this (what I assumed would work) failed attempt and do not really understand the actual differences that keep it from working.

Taking 2 listboxes (ListBox1 and ListBox2) with listbox2 containing items.

I can populate listbox1 with the contents of listbox2 using:

ListBox1.Items.AddRange(ListBox2.Items)

This of course populates listbox1 with the items in listbox2

What I thought would be just as easy a solution. I wanted to only add the selectedItems so I tried:
ListBox1.Items.AddRange(ListBox2.SelectedItems)
where I was then given the error:

Quote

Error 1 Overload resolution failed because no accessible 'AddRange' can be called with these arguments:
'Public Sub AddRange(items() As Object)': Value of type 'System.Windows.Forms.ListBox.SelectedObjectCollection' cannot be converted to '1-dimensional array of Object'.
'Public Sub AddRange(value As System.Windows.Forms.ListBox.ObjectCollection)': Value of type 'System.Windows.Forms.ListBox.SelectedObjectCollection' cannot be converted to 'System.Windows.Forms.ListBox.ObjectCollection'. C:\Users\Charlie\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 5 9 WindowsApplication1


I guess where I'm confused is why the SelectedObjectCollection cannot be converted to ObjectCollection. Wouldn't the first just be a subset of the objectcollection?

I did find a solution where I didn't have to create a loop (found not discovered ;)/>/>)

Here is the code that allows it to happen
ListBox1.Items.AddRange(ArrayList.Adapter(ListBox2.SelectedItems).ToArray)

So can someone enlighten me on what I'm apparently missing here? I understand the solution basically wraps the selecteditems to an array but I fail to understand why it is necessary.

This post has been edited by CharlieMay: 28 March 2013 - 06:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: ObjectCollection vs SelectedObjectCollection

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2290
  • View blog
  • Posts: 9,529
  • Joined: 29-May 08

Re: ObjectCollection vs SelectedObjectCollection

Posted 28 March 2013 - 07:10 PM

SelectedObjectCollection implements the IEnumerable interface so why not just
do?
ListBox1.Items.AddRange(ListBox2.SelectedItems.ToArray())


Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1719
  • View blog
  • Posts: 5,636
  • Joined: 25-September 09

Re: ObjectCollection vs SelectedObjectCollection

Posted 28 March 2013 - 07:26 PM

Adam, I tried that but .ToArray is not a member of the SelectedObjectCollection. Which is why I went searching.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2290
  • View blog
  • Posts: 9,529
  • Joined: 29-May 08

Re: ObjectCollection vs SelectedObjectCollection

Posted 28 March 2013 - 07:36 PM

You also ruled out .Cast(Of T)?
Was This Post Helpful? 1
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2290
  • View blog
  • Posts: 9,529
  • Joined: 29-May 08

Re: ObjectCollection vs SelectedObjectCollection

Posted 28 March 2013 - 08:01 PM

On the inheritance question.
I think SelectedItems is a ReadOnly and a fixed sized collection.
It is implemented as an inner class of the list box, i suspect to have access to private members like the array containing the items.
Was This Post Helpful? 1
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1719
  • View blog
  • Posts: 5,636
  • Joined: 25-September 09

Re: ObjectCollection vs SelectedObjectCollection

Posted 29 March 2013 - 04:37 AM

Ah yes, Cast(of T) seems a lot cleaner too. At least in my case, it's easier to read and follow.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2290
  • View blog
  • Posts: 9,529
  • Joined: 29-May 08

Re: ObjectCollection vs SelectedObjectCollection

Posted 29 March 2013 - 06:22 PM

I also learnt that the LINQ extension methods expected IEnumerable(Of T) and not IEnumerable
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1