0

I'm struggling to solve a small bit of code. What the code does is to first load a CSV file, line by line (starting by line 3), and add it to an array. Then run a regex match and I want to insert the value in an array.

This is my working code, it shows a msgbox with the actual matches:

Dim file = "C:\Users\David\Desktop\mycsv.csv"
    Dim basestatisticspath = Path.GetDirectoryName(file)
    Dim statistics() As String = IO.File.ReadAllLines(file)
    'Dim x As Integer = statistics.Length
    'ReDim Preserve statistics(x)

    Dim regexlang As Regex = New Regex("(?<=^"")\[.*\]")
    Dim regexlinefilename As Regex = New Regex("(?<=^""\[.*?\]\s).*(?="")")
    Dim linefilename As Match = Nothing
    Dim langmatch As Match = Nothing
    Dim filename() As String
    Dim lang() As String

    For i = 2 To UBound(statistics)
        langmatch = regexlang.Match(statistics(i))
        linefilename = regexlinefilename.Match(statistics(i))
        MsgBox(langmatch.Value & linefilename.Value)
    Next

That works well and the actual matches is what I want. So my next step was to add each match to an array to then use it to generate other files.

Therefore I ended up with this:

    Dim file = "C:\Users\David\Desktop\myscv.csv"
    Dim basestatisticspath = Path.GetDirectoryName(file)
    Dim statistics() As String = IO.File.ReadAllLines(file)
    'Dim x As Integer = statistics.Length
    'ReDim Preserve statistics(x)

    Dim regexlang As Regex = New Regex("(?<=^"")\[.*\]")
    Dim regexlinefilename As Regex = New Regex("(?<=^""\[.*?\]\s).*(?="")")
    Dim linefilename As Match = Nothing
    Dim langmatch As Match = Nothing
    Dim filename() As String
    Dim lang() As String

    ' Set all value line by line
    For i = 2 To UBound(statistics)
        langmatch = regexlang.Match(statistics(i))
        linefilename = regexlinefilename.Match(statistics(i))
        lang(i) = langmatch.Value.ToString
        filename(i) = linefilename.Value.ToString
        MsgBox(langmatch.Value & linefilename.Value)

    Next

After adding the below the program crashes on that line

    lang(i) = langmatch.Value.ToString
    filename(i) = linefilename.Value.ToString

I am assuming you can add the value of a regex match to a certain position of a string, but it seems I am wrong.

I´ve been searching for an answer with no results (at least to my poor understanding).

How could I convert each of the matches to a string and add it to the i position of the array?

Thanks in advance!

UPDATE: As @Tval explained, I solved it by including the size of the array when declaring it. Thanks!

    Dim filename(UBound(statistics)) As String
    Dim lang(UBound(statistics)) As String
12
  • 1
    Is it crashing or throwing an error? Have you assigned lang() and filename() a value or are they null? Commented Mar 15, 2018 at 14:28
  • @tval I get this: System.NullReferenceException occurred HResult=0x80004003 Message=Object reference not set to an instance of an object. Both lang() and filename() are empty, just declared them on this form. Commented Mar 15, 2018 at 14:38
  • I would assume it's because you have not initialized the size of the array. Try this: Dim lang(UBound(statistics)) and Dim filename(UBound(statistics)) Commented Mar 15, 2018 at 14:40
  • If it's throwing an exception, what's the error message? As @tval said, unless you didn't share all of your code, the problem seems to be that you never set the length of the lang and filename arrays, so they are still Nothing. Try changing it to Dim filename(UBound(statistics)) As String and Dim lang(UBound(statistics)) As String to preallocate them to the necessary length. Commented Mar 15, 2018 at 14:40
  • @tval, thank you very much! That fixed it and now it´s working as expected (I will update the initial questions for future reference). I actually tried your suggestion before posting here and it didn´t work, I guess I did something wrong... Thank you very much for your help! Commented Mar 15, 2018 at 15:35

1 Answer 1

1

you need to initialize the array before you can reference it or you'll get a null reference error, also you can't reference an index that doesn't exist yet or you'll get an index out of range exception.

right now your using an array with a fixed length, so if you want to add a value to it you'll have to re-declare it one index larger every time.

If you want an array of a variable length id suggest using a list, so you can just append values to it without any issues

Dim myList = New List(Of String)
For Each foo As String In bar
    myList.Add(bar)
Next
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @doom87er I will do it going forward and practice the same exercise with a list.

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.