0

When running debugger, i am getting the following error,

A first chance exception of type 'System.IndexOutOfRangeException' occurred in HSL File Config.exe Additional information: Index was outside the bounds of the array. An unhandled exception of type 'System.IndexOutOfRangeException' occurred in HSL File Config.exe Additional information: Index was outside the bounds of the array. The program '[14044] HSL File Config.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

i have checked the csv data file im loading is in fact 30 columns long and no blank lines. there are however some blank fields.

    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) = "R62167" Then
            ReDim Preserve fields(fields.Length)
            fields(31) = "9991"
        End If
        If fields(0) = "R62193" Then
            ReDim Preserve fields(fields.Length)
            fields(32) = "1999"
        End If
        If fields(2) = "2249" Then
            fields(2) = "0000"
        End If

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

2 Answers 2

1

Use redim

If fields(5) = "4WK" Then
    ReDim Preserve fields(fields.Length)
    fields(6) = "NewDate"
End If
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Mukul Varshney, you have saved my bacon! amazing. cant believe it was something so simple :)
thanks for the info, but getting errors with larger data sets (around 30 columns) A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Will suggest you to define fields array with an expected maximum columns, outside the for loop. use a variable count to update the fields value. fields(counter) = "NewDate" counter = counter +1, once you add a value, or declare fields as a List. This will be similar to what @FloatingKiwi suggested.
0

Instead of using Fields as an array try it as a list as these are resizable.

Dim fields = line.Split(","c).ToList()

then you can do

If fields(5) = "4WK" Then
    fields.Add("NewDate")
End If

8 Comments

Thanks both. this seems to work well with small amounts of data, but once i try it with the real file im trying to manipulate i get A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll errors any ideas? code snippit to follow
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 ' version 1 Dim fields = line.Split(","c) Dim fields = line.Split(","c).ToList() If fields(28) = "ZIPSPEED" Then fields.Add("NewDateD") End If output.Add(String.Join(","c, fields)) Next File.WriteAllLines(fileout, output)
it seems to fail on any field other than fields(0) if that helps?
I suspect you have a blank line in your file so splitting it yields just a blank string. So test whether your fields has at least 29 fields before processing the fields. Please get acquainted with the debugger as these issues are quicker to fix yourself than posting on SO and waiting for a reply
Your asking for an element that doesn't exist. When you split the string you didn't end up with your expected number of strings. Use a debugger and look at the values, it'll be blatantly obvious what is wrong.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.