inzo21 Posted December 18, 2003 Posted December 18, 2003 Hello all, I'm trying to solve the following predicament. I need to pass the information contained in a collection of checked items stored in a listview component to an array of strings. The particular issues I am having is the conversion part. I was able to pull the information out of the listview using the following iteration: Dim myarray() As String = {} Dim item As ListViewItem = New ListViewItem Dim s As String = Nothing Dim checklist As ListView.CheckedListViewItemCollection = filelistview.CheckedItems Dim counter As Integer = 0 For Each item In checklist myarray.SetValue((item.SubItems(2).Text.ToString), counter) Debug.WriteLine(myarray.GetValue(counter) & " -- " & counter) counter += 1 Next the error I receive is an arrayindex out of bounds error. Not sure how to overcome this. I used used an arraylist for the object to hold the items and it seemed to work fine. I'm sure it's somthing simple - maybe I'm just staring at it too long. Thus, the question: How do I either store these items into the string array without throwing the out of bounds exception OR how can I convert the arraylist into an string array. Thanks in advance. inzo Quote he who forgets will be destined to remember... (E.Vedder)
*Experts* Bucky Posted December 18, 2003 *Experts* Posted December 18, 2003 You need to resize the myarray before you can add stuff to it. It'd be much easier to just use an ArrayList, or, even more specialized, a StringCollection: Dim checkedValues As New System.Collections.Specialized.StringCollection() Dim item As ListViewItem ' You don't need the new keyword here Dim checklist As ListView.CheckedListViewItemCollection = filelistview.CheckedItems For Each item In checklist checkedValues.Add(item.SubItems(2).Text) Debug.WriteLine(checkedValues(checkedValues.Length - 1) & " -- " & checkedValues.Length - 1 Next Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
inzo21 Posted December 18, 2003 Author Posted December 18, 2003 final update Hey Bucky, Thanks a lot - I didn't know you needed to specify a zie for a string array. I plugged in the string collection class and it worked. I added the following code to convert the collection to an array of strings, in case anyone else is following along. I needed to do this because I could not control the type of information this needed to be passed as (i.e. convert it to a collection). Dim templist() As String ReDim templist(checkedValues.Count - 1) Dim s As String = Nothing Dim counter As Integer = 0 For Each s In checkedValues templist(counter) = s Debug.WriteLine(templist(counter).ToString) counter += 1 Next thanks again and happy holidays! inzo Quote he who forgets will be destined to remember... (E.Vedder)
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.