JustAnAverageSQLuser JustAnAverageSQLuser - 1 month ago 6x
Vb.net Question

VB.NET reading CSV and adding new fields conditionally

here I have a simple CSV file that I am reading and displaying into a textbox. I am using current data and replacing it with a simple IF statement. the problem I have is the Fields(5) statement where I want to declare a new field Fields(6) which doesn't exist. sorry this is probably obvious but I'm a self taught newbie!

Dim FileName = tbOpen.Text
Dim fileout = tbSave.Text
Dim lines = File.ReadAllLines(FileName)
Dim output As New List(Of String)

For Each line In lines
Dim fields = line.Split(","c)
If fields(0) = "012" Then
fields(0) = "999"
End If
If fields(1) = "abc" Then
fields(1) = "Norman"
End If

If fields(3) = "Y" Then
fields(3) = "Blue Leather"
End If
If fields(4) = "Y" Then
fields(4) = "Interlined"
End If
If fields(5) = "4WK" Then
fields(6) = "NewDate"
End If

output.Add(String.Join(","c, fields))
File.WriteAllLines(fileout, output)

Thanks in advance,


Use redim

If fields(5) = "4WK" Then
    ReDim Preserve fields(fields.Length)
    fields(6) = "NewDate"
End If