Results 1 to 13 of 13

Thread: storing list into an array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    20

    storing list into an array

    Hi, i have a csv file with two columns. column on is the name of a set of tools, and column 2 has the list of tools in each set.

    ex.

    Name of Set Names in set
    Good number1
    Good number2
    Good number3
    Bad number4
    Bad number5
    etc.

    So far I have code that will separate the two columns and put them into listboxes. The first listbox contains only one of the set names, not all the multiple names since that would be redundant. The second listbox contains all the names of the set. However, what I want to do is when set "Good" is selected, only the names in the set appear in the 2nd listbox. How could I do that?

    The code i have now to separate the lists is this...

    Dim strPath As String
    Dim SR As StreamReader

    strPath = "C:\Desktop\csv1.csv"

    SR = New StreamReader(strPath)

    Do While SR.Peek() <> -1
    Dim strfilestring As String = SR.ReadLine

    If strfilestring <> "" Then
    Dim filetext() As String = Split(strfilestring, ",")
    Dim found As Boolean = False

    For i As Integer = 0 To ListBox1.Items.Count - 1
    If ListBox1.Items.Item(i).ToString = filetext(0) Then
    found = True
    Exit For
    End If
    Next

    If found = False Then
    ListBox1.Items.Add(filetext(0))
    End If

    ListBox2.Items.Add(filetext(1))
    End If
    Loop

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Pardon my ignorance but what is a "csv" file?

    Apart from that, by placing

    Dim filetext() As String = Split(strfilestring, ",")
    Dim found As Boolean = False

    in the loop you are creating many separate instances of them. You should either put the declarations outside of the loop and empty them each loop or keep them inside of the loop, disposing them at the end of the loop - but that is wasteful.

    From your code it seems that each line in the file contains the name of the tool set followed by a "," followed by the name of one tool. How many names of tool sets are there? If it is not too onerous, how about having an array for each different set? You could then fill ListBox2 in accordance with the selection made in ListBox1. You could fill the arrays using a Select Case loop testing the value of filetext(0)

    Is your present code working at all? If so what is the actual error/problem?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3
    Addicted Member
    Join Date
    Sep 2004
    Location
    Brooklyn
    Posts
    147
    Pardon my ignorance but what is a "csv" file?
    Comma Separated Values file. Simliar to a comma-delimited ASCII text file, except I believe that the standard format has only string values placed inside double quotes, numbers and dates are not surrounded by double quotes.
    Last edited by Qualm; Sep 10th, 2004 at 10:09 AM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    20
    Thanks for the reply. Right now, the code does work. And I could do it the way you supposed by having an array for each specific tool set. However, There are quite a few and I am trying to make it dynamic so that this list can be added onto. any suggestions?

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    You did not comment on the question of multiple instances of Filetext() and Found.

    Does the file contain the name of the the set and one tool, delimited by "," (my thanks to Qualm ) on each line, or is the name of the set followed by more than one tool?

    If there is only one tool to each line you could split the file, one line at a time into an array in the knowledge that each odd number element contained the set name and each even number the tool name. You could then allocate the set names to listbox1 by accessing the even numbers of the array (regard 0 as an even number) skipping duplicate names. Loop until the array upperbound-1 is reached. You then compare the selected Set name to the contents of the even numbered elements of the array and when a match is found, store the contents of the following array index (the next higher odd number) to the second listbox.

    If there is more than one tool to each line we will have to think a little more.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    20
    thanks again taxes. About your earlier comment, I put the two lines of code inside the loop because i need those actions to occur everytime it loops around. How would I make it more efficient outside?

    Also, from your second reply. Are you saying to put the whole list into one gigantic array, and then only populate listbox1. then from the selecteditem from listbox1, compare that to all the even numbered elements in the big array and when a match is found, populate the 2nd listbox with the odd numbered array, and increment up for every following tool in that set.

    so then would I have to do another streamreader that would this time instead of going line by line, just ReadToEnd? I"m assuming this would be the easiest.

    Woutd you be able to code out an efficient way to do this briefly? I think I understand what you are trying to say, but I'm not sure if I'm familiar with all the syntax. Thanks a lot!!!

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Looks like you have sorted this out in your new thread.

    If not post again under this one.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    20
    Taxes,

    Thanks once again. I set i = 0 but it still didnt work. There is still nothing that shows up in listbox2. here's the code again if you wanted to see... Thanks again.

    Dim strpath2 As String
    Dim SR2 As StreamReader

    strpath2 = "C:\csv1.csv"
    SR2 = New StreamReader(strpath2)

    Dim strfilestring2 As String = SR.ReadToEnd
    Dim filetext2() As String = Split(strfilestring2, ",")
    Dim j As Integer = 1

    For i As Integer = 0 To filetext2.GetUpperBound(i) - 1 Step 2
    If ListBox1.SelectedItems.Item(i).ToString = filetext2(0) Then
    ListBox2.Items.Add(filetext2(j))
    j += 2
    End If
    Next

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    HI,

    Sorry, In my rush to look at new houses here I missed a couple of other errors. Alter your code to (look very carefully!)

    VB Code:
    1. Dim strfilestring2 As String = SR.ReadToEnd
    2. Dim filetext2() As String = Split(strfilestring2, ",")
    3.  
    4. For i As Integer = 0 To filetext2.GetUpperBound(i) - 1 Step 2
    5. If ListBox1.SelectedItems= filetext2(i) Then
    6. ListBox2.Items.Add(filetext2(i+1))
    7. End If
    8. Next
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    20
    Taxes,

    Thanks again for your reply. I made the changes that you pointed out, and still the code is not working. I don't know what the problem could be. I did change the 'selecteditems' to 'selecteditem'. there was an error with it plural. I don't know what else that could be wrong? Thanks so much again.

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Yes, sorry again, it should have been singular.

    Put a breakpoint on the line

    If ListBox1.SelectedItems= filetext2(i) Then

    and then check to see the following:

    1. What is contained in ListBox1.Selected Item

    2. What is the value of i

    3. What is the value of filetext2(i)

    4. What is the value of filetext2(i+1)

    Post the above values for the first, second and third times through the loop.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    20
    I finally got it taxes. Thanks again so much for your help. The problem with the code was that with .ReadToEnd, it didn't handle the next line because in a csv file, there is a comma that separates each cell, but not a new line. This is the new code that i have, familiar to the original one that i had.

    Dim strpath2 As String
    Dim SR2 As StreamReader

    strpath2 = "C:\csv1.csv"
    SR2 = New StreamReader(strpath2)
    ListBox2.Items.Clear()
    Do While SR2.Peek() <> -1
    Dim strfilestring2 As String = SR2.ReadLine

    If strfilestring2 <> "" Then
    Dim filetext2() As String = Split(strfilestring2, ",")

    If ListBox1.SelectedItem = filetext2(0) Then
    ListBox2.Items.Add(filetext2(1))
    End If
    End If
    Loop

    I did the .ReadLine again, and just looped through the whole array list. But I appreciate all the help you gave me!!!!

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    I think you will find that your latest code does NOT work. In:

    If ListBox1.SelectedItem = filetext2(0) Then
    ListBox2.Items.Add(filetext2(1))

    It will work only if the selected Set name is the same as the contents of filetext2(0).


    You are comparing the same element, filetext2(0) each time and not running through each of the sets.

    Also it is not necessary to read the file every time you select a new set.

    I think your entire code would be better as:

    With formwide scope

    VB Code:
    1. Dim filetext1() As String
    2. Dim Icount As Integer

    In the event which fills the first ListBox

    VB Code:
    1. Dim strpath2 As String
    2. Dim SR2 As StreamReader
    3. strpath2 = "C:\csv1.csv"
    4. SR2 = New StreamReader(strpath2)
    5. ListBox2.Items.Clear()
    6. Dim strfilestring2 As String = SR2.ReadLine
    7.  
    8. If strfilestring2 <> "" Then
    9.    filetext1() = Split(strfilestring2, ",")
    10.    For iCount=0 to filetext1.GetUpperBound-1.Step 2
    11.       ListBox1.Items.Add(filetext1(i))
    12.    Next
    13. End If

    Then

    VB Code:
    1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    2.    For iCount=0 to filetext1.GetUpperBound(0)-1 Step 2
    3.       If ListBox1.SelectedItem = filetext1(iCount) Then
    4.          ListBox2.Items.Add(filetext1(iCount+1))
    5.       End If
    6.    Next
    7.  
    8. End Sub
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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