3

I cannot figure out how to set an array to one of two sets of numbers (there will be more later), every way that I have tried throws some kind of error. I have tried to Dim the array inside the case statements, but then I cannot use the array in the For Each, which makes this worthless.... any ideas would be appreciated.

Code:

Dim HourArray() As Integer

Select Case CurrentShapeRow(ROW_PERIOD)
    Case "ON", "2X16"
        HourArray = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}
    Case "2X8", "5X8"
        HourArray = {0, 1, 2, 3, 4, 5, 22, 23}
    Case Else
        Throw New Exception(String.Format("Unhandled Period: {0}", CurrentShapeRow(ROW_PERIOD)))
End Select


For Each HourCount As Integer In HourArray()
     'DO SOME STUFF HERE
Next

3 Answers 3

5
HourArray = New Integer() {1,2,3,4,5,6,7,8,9}
Sign up to request clarification or add additional context in comments.

Comments

4

When you assign an array to an existing variable you must use a constructor explicitly:

HourArray = New Integer() { 6, 7, 8, 9, 10, 11, 12, 13 }

This differs from a declaration and assignment where the constructor is optional:

Dim HourArray() As Integer = { 6, 7, 8, 9, 10, 11, 12, 13 }

2 Comments

Thanks to you both, that worked, I figured it was something really easy that my brain just did not want to do today. +1 vote to both (Mark and Ben) and will accept Mark's when it allows me only because he was first.
@IPX Ares: Actually I think I was second unfortunately.
2
    Dim hourArray As List(Of Integer)

    Select Case CurrentShapeRow(ROW_PERIOD)
        Case "ON", "2X16"
            hourArray.AddRange(New Integer() {6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21})
        Case "2X8", "5X8"
            hourArray.AddRange(New Integer() {0, 1, 2, 3, 4, 5, 22, 23})
        Case Else
            Throw New Exception(String.Format("Unhandled Period: {0}", CurrentShapeRow(ROW_PERIOD)))
    End Select

For Each i As Integer In hourArray
    Console.WriteLine(i)
Next

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.