0

If I wanted to read the following from a CSV file into 4 separate arrays how would I proceed?
Ex:

John,Doe,100,98
Jane,Smith,90,90

I need to take a student's First & Last Name. The first grade is their midterm, the second is their final.

    Dim Name As String = ""
    Dim inFile As StreamReader
    Dim outFile As StreamWriter

    inFile = File.OpenText("grades.csv")
    outFile = File.CreateText("report.txt")


    Do While (Not inFile.EndOfStream)
        Name = CStr(inFile.ReadToEnd)
        outFile.WriteLine(Name)
    Loop

    inFile.Close()
    outFile.Close()

I see a million different ways to split things, I only output the file to see what I'm getting. Can someone help me split these into separate arrays? Thanks

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
LamarZ
  • 1
  • 1
  • 2
  • 3
  • 1
    *Why* would you want to store related data separately, *especially* the first and last name? – Ňɏssa Pøngjǣrdenlarp Mar 03 '16 at 00:01
  • I'll be taking the names and storing them into a list box. So when you click through you can generate a report/ see their grade. – LamarZ Mar 03 '16 at 00:14
  • 1
    As @Plutonix points out, it isn't a good idea to store related data in different places. The typical way to solve this type of problem is to create a Student class with properties for FirstName, LastName, MidtermGrade and FinalGrade. Then you can create a Student object from each line in the CSV file and add the objects to a `List(Of Student)`. You might want to display the data in a `ListView`, or a `DataGridView`. – Blackwood Mar 03 '16 at 00:16
  • [This answer may help you](http://stackoverflow.com/a/34164458/1070452) with the modern replacement for parallel arrays – Ňɏssa Pøngjǣrdenlarp Mar 03 '16 at 00:26
  • @LamarZ Don't forget to select\up-vote an answer.... – Monty Mar 19 '16 at 21:39

1 Answers1

0

Try this..... An example that might give you some ideas....

Imports System.Data.OleDb

Public Class Form1

Private dt As New DataTable

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim folder = "<Path to the folder of your grades.csv file (don't include the file name, just the path up to the folder)>"
    Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
    ' in the next line replace grades.csv with the name of your file....
    Using Adp As New OleDbDataAdapter("select F1 + ' ' + F2 as FirstSecondName, F3 as MidTerm, F4 as Final from [grades.csv] ", CnStr)
        Try
            Adp.Fill(dt)
        Catch ex As Exception

        End Try
    End Using
    Me.ListBox1.DataSource = dt
    ListBox1.DisplayMember = "FirstSecondName"
    ListBox1.ValueMember = "FirstSecondName"

    AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim senderListBox As ListBox = sender
    If senderListBox.SelectedIndex <> -1 Then
        Dim SelectedData As DataRowView = senderListBox.SelectedItem
        MessageBox.Show(String.Format("Selected - {0} :: Mid-Term Result = {1} :: Final Result = {2}", SelectedData("FirstSecondName").ToString, SelectedData("MidTerm").ToString, SelectedData("Final").ToString))
    End If
End Sub

End Class
Monty
  • 1,534
  • 2
  • 10
  • 12