簡體   English   中英

如何在VB.NET中將數組設置為值列表?

[英]How to set an array to a list of values in VB.NET?

我無法弄清楚如何將數組設置為兩組數字之一(稍后會有更多),我嘗試的每種方式都會引發某種錯誤。 我試圖在case語句中調暗數組,但后來我不能在For Each中使用數組,這使得它毫無價值......任何想法都會受到贊賞。

碼:

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

將數組分配給現有變量時,必須顯式使用構造函數:

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

這與構造函數是可選的聲明和賦值不同:

Dim HourArray() As Integer = { 6, 7, 8, 9, 10, 11, 12, 13 }
    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM