疑難排解陣列 (Visual Basic)Troubleshooting Arrays (Visual Basic)

此頁面會列出使用陣列時所發生的一些常見問題。This page lists some common problems that can occur when working with arrays.

宣告和初始化陣列的編譯錯誤Compilation Errors Declaring and Initializing an Array

從 宣告、 建立和初始化陣列的規則的誤解,可能會發生編譯錯誤。Compilation errors can arise from misunderstanding of the rules for declaring, creating, and initializing arrays. 錯誤的最常見的原因如下所示:The most common causes of errors are the following:

  • 提供New 運算子陣列變數宣告中指定的維度長度之後的子句。Supplying a New Operator clause after specifying dimension lengths in the array variable declaration. 下列程式碼行顯示此類型的無效的宣告。The following code lines show invalid declarations of this type.

    Dim INVALIDsingleDimByteArray(2) As Byte = New Byte()

    Dim INVALIDtwoDimShortArray(1, 1) As Short = New Short(,)

    Dim INVALIDjaggedByteArray(1)() As Byte = New Byte()()

  • 指定維度的長度超過一個最上層陣列的不規則陣列。Specifying dimension lengths for more than the top-level array of a jagged array. 下列程式碼行顯示此類型的無效的宣告。The following code line shows an invalid declaration of this type.

    Dim INVALIDjaggedByteArray(1)(1) As Byte

  • 省略New關鍵字指定的項目值時。Omitting the New keyword when specifying the element values. 下列程式碼行顯示此類型的無效的宣告。The following code line shows an invalid declaration of this type.

    Dim INVALIDoneDimShortArray() As Short = Short() {0, 1, 2, 3}

  • 提供New子句但不含大括號 ({})。Supplying a New clause without braces ({}). 下列程式碼行顯示此類型的無效的宣告。The following code lines show invalid declarations of this type.

    Dim INVALIDsingleDimByteArray() As Byte = New Byte()

    Dim INVALIDsingleDimByteArray() As Byte = New Byte(2)

    Dim INVALIDtwoDimShortArray(,) As Short = New Short(,)

    Dim INVALIDtwoDimShortArray(,) As Short = New Short(1, 1)

存取超出範圍的陣列Accessing an Array Out of Bounds

初始化陣列的程序將給每個維度的上限和下限。The process of initializing an array assigns an upper bound and a lower bound to each dimension. 每個存取陣列的項目必須指定有效的索引或註標,每個維度。Every access to an element of the array must specify a valid index, or subscript, for every dimension. 如果任何索引是低於其下限或高於其上限,IndexOutOfRangeException例外狀況結果。If any index is below its lower bound or above its upper bound, an IndexOutOfRangeException exception results. 編譯器無法偵測這類錯誤,因此在執行階段發生錯誤。The compiler cannot detect such an error, so an error occurs at run time.

判斷範圍Determining Bounds

如果另一個元件會將陣列傳遞至您的程式碼,例如做為程序引數中,您不知道該陣列的大小或其維度的長度。If another component passes an array to your code, for example as a procedure argument, you do not know the size of that array or the lengths of its dimensions. 您嘗試存取任何項目之前,您一律應該判斷陣列的每個維度的上限。You should always determine the upper bound for every dimension of an array before you attempt to access any elements. 如果已建立 Visual Basic 以外的其他方式的陣列New子句,下限看起來可能不是 0,而且它是最安全的方式決定也該下限。If the array has been created by some means other than a Visual Basic New clause, the lower bound might be something other than 0, and it is safest to determine that lower bound as well.

指定的維度Specifying the Dimension

在決定多維度陣列的界限,負責指定維度的方式。When determining the bounds of a multidimensional array, take care how you specify the dimension. dimension的參數GetLowerBoundGetUpperBound方法是以 0 為基礎,同時Rank參數的 Visual BasicLBoundUBound函式是以 1 為基礎。The dimension parameters of the GetLowerBound and GetUpperBound methods are 0-based, while the Rank parameters of the Visual Basic LBound and UBound functions are 1-based.

另請參閱See also