reading and writing to csv file

Aidanb79

Member
Joined
Feb 14, 2010
Messages
10
Programming Experience
Beginner
Hi All,

I am working on a Yahtzee projeect for college and am struggling with reading and writing to a csv file the code i have to read the file works but only gets the first column and displays it on a line not as a list.
VB.NET:
Private Sub HighScoreFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.Columns.Add("ID")
        ListView1.Columns.Add("User")
        ListView1.Columns.Add("Score")
        ListView1.GridLines = True


        Using TFP As New FileIO.TextFieldParser(Application.StartupPath & "\Score.csv")

            TFP.TextFieldType = FileIO.FieldType.Delimited
            TFP.SetDelimiters(",")

            While Not TFP.EndOfData
                Try
                    Dim CR As String() = TFP.ReadFields()
                    Dim lvi As New ListViewItem(CR)
                    ListView1.Items.Add(lvi)
                Catch ex As Exception
                    MessageBox.Show("Cannot parse file." + ex.Message)
                End Try
            End While
        End Using

    End Sub

the other criteria i have to work towards and wondering if anyone can help is to limit the amount of records to 10 so need to check what the lowest score is and if the current score is higher over write it, any help would be appriciated.

Regards

Aidan
 
Thank you jmcilhinney that worked,

do you know how i would be able to extract the lowest score and if the current score is higher to over write it?

Regards

Aidan
 
As you're using .NET 3.5, the simplest option is to use LINQ, e.g.
VB.NET:
Dim lowestItemWithValue = (From lvi In Me.ListView1.Items.Cast(Of ListViewItem)()
                           Let val = CInt(lvi.Text)
                           Order By val
                           Select New With {.Item = lvi, .Value = val}).First()

If someValue < lowestItemWithValue.Value Then
    lowestItemWithValue.Item.Text = someValue.ToString()
End If
 
Hi,

Thanks for the reply i have used the code you submited and i keep getting the error
") expected" on the "Val" on Val=CInt(Lvi.Text) i cant get rid of the error no matter where i put the closing parenthesis.

VB.NET:
Dim lowestItemWithValue = (From lvi In Me.ListView1.Items.Cast(Of ListViewItem)() _
                            Val= CInt(lvi.Text) _
                            Order by val _ 
                            Select New With {.Item = lvi, .Value = val}).First()

    Private Sub lowScore()
        If SaveScorefrm.LblScore.Text < lowestItemWithValue.Value Then
            lowestItemWithValue.Item.Text = SaveScorefrm.LblScore.Text.ToString()
        End If
    End Sub

Regards

Aidan
 
Read my code again and compare it to yours. I did leave out the line continuation characters as I was using VB 2010, so sorry about that. Other than that, do you see any other differences between them, particularly around where the error occurs?
 
Back
Top