3

I am trying to split a string and take the split parts and assign them to variables. This works fine with the first variable but I cannot get the second variable to work.

For Each columnQuarter As DataColumn In dt.Columns
            Dim s As String = columnQuarter.ColumnName
            Dim words As String() = s.Split("-")
            Dim Year As String = words(0)
            Dim Quarter As String = words(1)
            Debug.WriteLine(Year)
            Debug.WriteLine(Quarter)

At line "Dim Quarter As String = words(1)" I get error code "IndexOutofRangeException was unhandled. Index was outside the bounds of the array."

An example of a ColumnName would be 2012-Q1.

ADDITIONAL INFORMATION:

Here is the code that I use to add my columns to my dataTable. As you can see my first two columns do not contain "-".

tickerColumn = New DataColumn("Ticker", Type.GetType("System.String"))
consistencyColumn = New DataColumn("Consistency", Type.GetType("System.Int32"))
dt.Columns.Add(tickerColumn)
dt.Columns.Add(consistencyColumn)

    With Me
        lr = Now.Year - 1901
        For i = 1 To lr
            column = New DataColumn
            column.DataType = System.Type.GetType("System.Int32")
            column.ColumnName = Now.Year - i & "-Q4"
            dt.Columns.Add(column)

            column = New DataColumn
            column.DataType = System.Type.GetType("System.Int32")
            column.ColumnName = Now.Year - i & "-Q3"
            dt.Columns.Add(column)

            column = New DataColumn
            column.DataType = System.Type.GetType("System.Int32")
            column.ColumnName = Now.Year - i & "-Q2"
            dt.Columns.Add(column)

            column = New DataColumn
            column.DataType = System.Type.GetType("System.Int32")
            column.ColumnName = Now.Year - i & "-Q1"
            dt.Columns.Add(column)
        Next i
    End With
10
  • 2
    What is the length of words after you call Split("-")? And what is the value you get for words(0)? Commented Feb 1, 2013 at 15:07
  • 1
    Have you verified that all columns in your DataTable have a hyphen in them? That exception means that there was not a second element in the split array. Commented Feb 1, 2013 at 15:13
  • 1
    I mean what is the length of the string array that you have named words. I.e. what does Debug.WriteLine(words.Length) give? Commented Feb 1, 2013 at 15:13
  • 1
    Your code works fine. Make sure the value is indeed in the format "2012-Q1". If the format looks OK, then maybe the dash in the string is not a standard dash... Commented Feb 1, 2013 at 15:13
  • 1
    also what is the value of your columnQuarter.ColumnName when your exception is thrown? Try adding Debug.WriteLine(columnQuarter.ColumnName) just before you declare words Commented Feb 1, 2013 at 15:16

1 Answer 1

3

Since your first two columns do NOT have the dash, try filtering them out:

For Each columnQuarter As DataColumn In dt.Columns
  Dim s As String = columnQuarter.ColumnName
    If s.Contains("-") Then
      Dim words As String() = s.Split("-")
      Dim Year As String = words(0)
      Dim Quarter As String = words(1)
      Debug.WriteLine(Year)
      Debug.WriteLine(Quarter)
    End If
Next
Sign up to request clarification or add additional context in comments.

Comments

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.