Help populating array with checklistbox
I made a quick search but couldn't find any help
I need to put the values of a checklist box into an array. I've found several solutions on the internet but all of these don't seem to work for integer arrays. This is what I've got so far
" Dim RemoveValues(checklistRemoveBrew.CheckedItems.Count - 1) As Integer
Dim C As Integer
For C = 0 To checklistRemoveBrew.CheckedItems.Count - 1
RemoveValues(C) = checklistRemoveBrew.CheckedItems(C)
Next "
Thanks!
Re: Help populating array with checklistbox
Just to clarify - are you only wanting to put the checked items into the array as that is what your current code would do, but not what your question says.
The problem I can see is you are trying to stuff an object into an integer - the checkeditemscollection doesn't contain integers, but objects so you need to cast the item to an integer. You can use the cint() function, integer.tryparse method or a number of other methods to do that.
Re: Help populating array with checklistbox
Quote:
Originally Posted by
keystone_paul
Just to clarify - are you only wanting to put the checked items into the array as that is what your current code would do, but not what your question says.
The problem I can see is you are trying to stuff an object into an integer - the checkeditemscollection doesn't contain integers, but objects so you need to cast the item to an integer. You can use the cint() function, integer.tryparse method or a number of other methods to do that.
How do i use the cint() function?
Thanks
Re: Help populating array with checklistbox
RemoveValues(C) = cint(checklistRemoveBrew.CheckedItems(C))
You should note though that this will raise an exception if it can't be converted to an integer.
Re: Help populating array with checklistbox
Try this one line of code:
Code:
Dim RemoveValues() As Integer = Array.ConvertAll(Of Object, Integer)(checklistRemoveBrew.CheckedItems.Cast(Of String)().ToArray, AddressOf Convert.ToInt32)