Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Visual basic code problem?

I am trying to read data from a csv file into a collection but not having much luck. Could someone tell me whats wrong with my code. It needs to be split as well that complicates things as the comments indicate. Thanks in advance.

Imports System.IO

Public Class test

Private Sub btnGetData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetData.Click

Dim sr As StreamReader = New StreamReader("parts.csv")

Dim storagelist As New List(Of String)

Dim numarray(0) As String

Try

' Trying to split the data

'numarray = sr.ReadLine.Split(",")

' trying to add the split data to the collection.

'For Each w In numarray

'storagelist.Add(numarray(w))

'Next

storagelist.Add(sr.ReadLine())

sr.Close()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

''''''''''''''''''

For Each stri As String In storagelist

MsgBox(stri)

Next

End Sub

End Class

Here is an example of the data that needs reading in it if helps:

AB3476, Regular Brick, 5

AB3211, Long Brick , 10

AB2211, Roof Brick, 0

AB2501, Door Brick, 2

2 Answers

Relevance
  • MarkG
    Lv 7
    1 decade ago

    Try not to do too much in any given line of code.

    for example:

    Trying to split the data

    'numarray = sr.ReadLine.Split(",")

    break this down into two distinct steps

    ' Read the line into a string

    strLine = sr.ReadLine

    'break up the CSV line into individual fields

    numArray = Split(strline , ",")

    By doing individual steps line by line of code you can now use debug mode to step through and see information that is otherwise hidden when combined into one line. You can now look at the extracted strLine and see exactly what has been read from teh csv file and if such data is what you expect. Consider if the first line of data read from the file is a null (blank line) instead of CSV data. If such a problem is happening you will never see it in debug mode in your original combined line of code.

    >>>Dim numarray(0) As String<<<

    You have declared a fixed array size with one element. The split function is expecting a dynamic array so don't include a number in the parenthesis

    Dim numarray() As String

    >>>

    'For Each w In numarray

    'storagelist.Add(numarray(w))

    'Next

    <<<

    You haven't declared a data type for w and you are relying upon VB to correctly assign a string to it.

    It doesn't cost you anything to use the keys on the key board , don't be afraid to type a little more

    For Each w As String In Numarray

    >>>'storagelist.Add(numarray(w))

    here you are using w as an index but the For Each has defined it as a string with the stored contents. You just need to Add the string using the w you just defined

    storagelist.Add(w)

  • Anonymous
    1 decade ago

    Much simpler to open the file for reading, read it into an array, close it, then handle the array. This old VB6 code will still work http://www.vbforums.com/showthread.php?t=410163 Use the second code block. s() is your data.

    For Each w As String in s

    numarray() = w.Split(",")

    should give you the elements of the line in numarray().

Still have questions? Get your answers by asking now.