How to set an array to a list of values ​​in VB.NET?

I can’t understand how to set the array to one of two sets of numbers (it will be later), every time I try, it gives some kind of error. I tried to output the array inside the case arguments, but then I cannot use the array in For Each, which makes it useless .... any ideas would be appreciated.

the 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
source share
3 answers
    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
+2
source
HourArray = New Integer() {1,2,3,4,5,6,7,8,9}
+5
source

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

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

This is different from declaration and assignment, where the constructor is optional:

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

Source: https://habr.com/ru/post/1740364/


All Articles