Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am trying to make a database for creating part numbers and am struggling to access strings in a list of lists. I have a .txt file that I am trying to load information from and into a list of lists. The first line contains the first available project number (in case a new project is added) and gets stored as newProjNum. Each line in the file after that is a list that contains information for individual projects as follows (where "first available" takes into account existing part numbers and returns the lowest unused number so that no number is redundant):

PI, Project name, Project number, first available assembly number, first available part number

I have also declared variables in a global file as follows:

VB
Public Property projPartNumInfo As New List(Of String)
Public Property allPartNumInfo As New List(Of List(Of String))
Public Property NewProjNum As Integer


My first check passes and prints out each of the five strings from the projPartNumInfo list, concatenated together, through a message box. I can't seem to retrieve those same strings again once I add the projPartNumInfo lists into the allPartNumInfo list of lists, though. I have tried implementing check 2 in three different ways and can't access the information in allPartNumInfo. Version 1 executes the message box command but with 4 new line characters and nothing more--it doesn't see any item's in each line so it gets nothing from the second for-loop. Versions 2 and 3 throw errors saying that the index is out of range as soon as I try and index into the allPartNumInfo list of lists.

VB
Private Sub loadAllPartNumInfo()
        'code used from https://msdn.microsoft.com/en-us/library/cakac7e6.aspx and altered

        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(partNumTextFile)
            MyReader.TextFieldType = FileIO.FieldType.Delimited
            MyReader.SetDelimiters(",")
            Dim currentRow As String()
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()
                    Dim currentField As String
                    For Each currentField In currentRow
                        If currentRow.Length = 1 Then
                            NewProjNum = currentField
                            Exit Try
                        Else
                            projPartNumInfo.Add(currentField)
                        End If
                    Next
                    '
                    'Check 1-checks whether proper strings are being added to projPartNumInfo list
                    '       -should print out "PI,project,projectNumber,firstAvailableAssemblyNum,firstAvailablePartNum"
                    '
                    'Dim result As String = ""
                    'For Each elem As String In projPartNumInfo
                    '    result &= elem & ","
                    'Next
                    'MsgBox(result)
                    '
                    'Check 1 cleared, prints four lines with five strings properly
                    '
                    allPartNumInfo.Add(projPartNumInfo)
                    projPartNumInfo.Clear()
                Catch ex As Microsoft.VisualBasic.
                            FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message &
                    "is not valid and will be skipped.")
                End Try
            End While
        End Using
        '
        'Check 2-checks whether proper projPartNumInfo lists are being added to allPartNumInfo list of lists
        '       -should print out same as debugger above, but with multiple lines for different projects, separated by a new line
        '
        'Version 1:
        'Dim sresult As String = ""
        'For Each line As List(Of String) In allPartNumInfo
        '    For Each item As String In line
        '        sresult &= item
        '    Next
        '    sresult &= vbNewLine
        'Next
        'MsgBox(sresult)
        '
        '
        'Version 2
        'Dim sresult2 As String = ""
        'Dim line As List(Of String) = Nothing
        'For x As Integer = 1 To 4
        '    line = allPartNumInfo(x)
        '    For y As Integer = 1 To 5
        '        Dim item As String = line(y)
        '        sresult2 &= (item & ",")
        '        item = ""
        '    Next
        '    sresult2 &= vbNewLine
        'Next
        'MsgBox(sresult2)
        '
        '
        'Version 3
        'Dim sresult3 As String = ""
        'For x As Integer = 1 To 4
        '    For y As Integer = 1 To 5
        '        sresult3 &= (allPartNumInfo(x)(y) & ",")
        '    Next
        '    sresult3 &= vbNewLine
        'Next
        'MsgBox(sresult3)
        '
        '
        'Check 2, Version 1 2 and 3 Failed
        '
    End Sub

Any suggestions or help? Let me know if anything isn't clear. Thanks in advance for all answers.
Posted

You did not even try to index it in your code.
Here is the hint for you: the list of lists is still a list, no different from any other element types. When you index a list of lists, you get a reference to the element of this list. And who cares that this element is also a list? You got a reference to it. And then, when you have a reference to an "inner" list object, you can index it to get a reference to its element, in your case, a string.

Only remember that to list is logically a jugged array. It means that each of the "inner" lists has different length. You need to check the Count property before you try to get anything by the index: https://msdn.microsoft.com/en-us/library/27b47ht3(v=vs.110).aspx[^].

You really need to learn how to write code by yourself. When asking question, try to show what have you tried so far.

Why I'm not writing any code for your this time? Here is why: Give a man a fish….
Thank you for your effort and understanding. This is your turn now.

—SA
 
Share this answer
 
v2
Comments
Member 11742116 4-Jun-15 13:45pm    
I did try and index it in my code, it is just commented out for now because I tried to implement the test in multiple ways and couldn't get any to work. Unless what you meant to say was that I am indexing it in the wrong way, in which case that was actually my initial question: how do I index into a list of lists?

For example, in version 2 of my check 2 which is commented out towards the bottom, the code
line = allPartNumInfo(x)
assigns the variable "line" a reference to the list of strings (an "inner" list object in this case, as you call it) in the x'th index of allPartNumInfo. I then try and index into "line" with
Dim item As String = line(y)
in an attempt to retrieve the string contained in my inner list. When I put in a break point and move through that line I get the following error:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
This is where I am stuck/confused. A similar error occurs when I walk through version 3, also commented out at the bottom, and run the line
sresult3 &= (allPartNumInfo(x)(y) & ",")
where I try and index into both the list and list-of-lists at once (as I have seen in some of my google travels.)
Sergey Alexandrovich Kryukov 4-Jun-15 14:51pm    
All right, you need to declare "line" as List(of String). Have you done that?
Then, yes, next line, item, is a string, element of the list referenced as "line".
Out of range? All right, indices should be 0 to line.Count - 1. If, say, you have 0 strings (line.Count is zero), there no item, any index will throws this exception.

So, next issues will be 1) did you assign a List(of String) object obtained using a list constructor to all elements of allPartNumInfo? did you call this constructor on each element? 2) if you did, did you add any String-type elements to each or some of the List(of String) objects, elements of allPartNumInfo? 3) if you did, did you initialize the string values with some value other than null (Nothing)?

Again the idea is: the element of list of lists is just the object, no different from other list elements, but this is the object of the List(of String) type...

—SA
It is like Sergey has written and explained in Solution 1.
If you have a List of List then your outer List.Item returns an object which represents the inner List. No more and not less ...
If you want to walk through then perhaps it works like this :
VB
public InnerList as List (of string)
public OuterList as List (of InnerList)

public Sub MySearcher

dim myList as List (of string)

for i as integer = 1 to OuterList.count
   myList = OuterList.Item(i-1)
   for j as integer = 1 to myList.count
      ' and now do what you are interested to do inside the InnerList
   next
next

end sub


I hope it helps ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900