Results 1 to 5 of 5

Thread: Help populating array with checklistbox

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    2

    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!

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    2

    Re: Help populating array with checklistbox

    Quote Originally Posted by keystone_paul View Post
    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

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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)
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width