0

For VB.net 2015

I'm a new programmer. I've been warping my brain around this for 2 days. Can seem to see the problem. It seems I can only add 150 records to the dictionary. I'm not sure where its failing in the code. I'm not getting any errors or warnings.

Really hope someone can give me a hand.

Heres a link to my file I'm working with.

https://drive.google.com/file/d/0B1zf86jcRv49Y1lCMHRvNWt4UFk/view?usp=sharing

P.S. Sry about the crappy coding skill :-)

Imports System
Imports System.IO


Public Class Form1
    Dim xx As Integer = 0
    Private MainDataList As New Dictionary(Of String, List(Of String))



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim temp1 As List(Of String)
        'MsgBox(xx)
        If MainDataList.ContainsKey(TextBox1.Text) Then
            temp1 = MainDataList.Item(TextBox1.Text)
            Label1.Text = temp1(0)
            Beep()
        Else
            Label1.Text = "Not Found"
            Beep()
            Beep()
        End If


        TextBox1.Text = ""


    End Sub

    Private Sub GetData()
        Dim ReadDataLine(50000) As String

        Try
            ' Open the file using a stream reader.
            Using sr As New StreamReader("C:\Inventory\Invatory.csv")
                'Dim line As String
                ' Read the stream to a string and write the string to the console.


                ReadDataLine(0) = sr.ReadLine
                Do While (sr.EndOfStream = False)

                    ReadDataLine(xx) = sr.ReadLine
                    'AddToList(ReadDataLine) ' pars data into main list
                    'MsgBox(sr.ReadLine)
                    xx = xx + 1


                Loop
                sr.Close()
                'line = sr.ReadLine()



            End Using
        Catch e As Exception
            'MsgBox(xx)
            MsgBox("The file could not be read:")
            MsgBox(e.Message)
        End Try
        'MsgBox(xx)

        'xx = 0
        For Each i As String In ReadDataLine

            AddToList(i)
            'MsgBox(xx)
            'xx = xx + 1
        Next



    End Sub
    Private Sub AddToList(data As String)

        Dim barCode As String = ""
        Dim steps As Integer = 1
        Dim LastPos As Integer = 2
        Dim datalist As New List(Of String)
        Dim firstPass As Boolean = False
        For i = 2 To Len(data)

            'Find end of cell
            If Mid(data, i, 1) = "," Then

                If firstPass = False Then
                    barCode = Mid(data, LastPos, i - 2)
                    'MsgBox(Mid(data, LastPos, i - 2))
                    LastPos = i
                    firstPass = True
                Else
                    Dim temp As Integer = i - LastPos
                    datalist.Add(Mid(data, LastPos + 1, temp - 1))
                    'MsgBox(Mid(data, LastPos + 1, temp - 1))
                    LastPos = i
                End If
            End If
        Next

        MainDataList.Add(barCode, datalist)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GetData()

        Label1.Text = "Ready!"
        Me.Show()
        TextBox1.Focus()



    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub
End Class
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • 2
    Lines 126 and 152 contain the same barcode but a dictionary requires unique keys. 152 minus one for a header row would mean that entry 151 is failing which means that there are 150 successful entries in the list. – Chris Haas Dec 10 '15 at 22:01
  • There are other duplicates too. 17 in all. @ChrisHaas Nice analysis. – JerryM Dec 10 '15 at 22:07
  • Ohh.. Yea THANKS!!! I'll put in the error correction code. Yea Did not think of that even I know it must be unique keys. Thanks again and great Job! – Brian Link Dec 10 '15 at 22:17
  • For a beginner to VB.Net I would recommend avoiding creating arrays at all costs. The syntax is weird and non-standard compared to other common languages and can get really confusing. The generic `List(of String)` is much more flexible and forgiving. Even as a professional with 15+ years experience I still avoid them whenever possible. Also, I would almost recommend never using try/catch. Sounds weird, I know, but especially during development, you want to see those errors and learn to test for those scenarios. – Chris Haas Dec 10 '15 at 22:21
  • Added: If MainDataList.ContainsKey(barCode) = False Then MainDataList.Add(barCode, datalist) Fix it right up. Just need to find the duplicates and print new barcodes for them. :-) – Brian Link Dec 10 '15 at 22:31
  • @Chris Haas Vary true. An array seemed to be the simplest way to go. But, something simple can get vary complexing fast. – Brian Link Dec 10 '15 at 22:33
  • I'd also recommend taking a look at the built-in parser that can handle CSV records called [`Microsoft.VisualBasic.FileIO.TextFieldParser`](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(v=vs.110).aspx) – Chris Haas Dec 10 '15 at 22:35
  • @ChrisHaas; "For a beginner to VB.Net I would recommend avoiding creating arrays at all costs". I couldn't agree less. If an array is the appropriate type for a situation then that's what you should use. If you need the functionality of a generic `List` then that's what you should create but a list of items that will not be resized should absolutely be in an array. – jmcilhinney Dec 11 '15 at 02:44
  • 1
    @ChrisHaas; "Also, I would almost recommend never using try/catch". I also disagree with that strenuously. You should never add an exception handler just in case, which may be what you're actually trying to say. If you identify a specific exception that can reasonably be thrown then you should absolutely handle it, but any other exceptions should be handled by the `UnhandledException` event handler of a WinForms application. You might choose to avoid handling that event until it's time to deploy. It's probably a good idea to wrap the event handler in conditional compilation statements. – jmcilhinney Dec 11 '15 at 02:48
  • @jmcilhinney, both comments were intended to be under the "for a beginner" clause, specifically targeting VB.Net. For most people the [perf difference between an array and other collections is negligible](http://stackoverflow.com/a/434765/231316). Also, I specifically said to avoid _creating_ them, but not necessarily using them, although I should have emphasized that more. What I mean is that almost every case of `Dim X(5)` that I've seen from beginners is them making an expectation early in their code and having it fail much later because of bounds or null issues. – Chris Haas Dec 11 '15 at 14:39
  • I admit to being a little overzealous with my `try/catch` rule but I still generally agree with it. For beginners, `try/catch` is a giant crutch that hides underlying problems, just like turning `Option Explicit` and/or `Option Strict` off. – Chris Haas Dec 11 '15 at 15:18

0 Answers0