# 疑難排解程式（Visual Basic）Troubleshooting procedures (Visual Basic)

## 從函數程式傳回陣列型別Returning an array type from a function procedure

Function AllOnes(n As Integer) As Integer()
For i As Integer = 1 To n - 1
' The following statement generates a COMPILER ERROR.
AllOnes(i) = 1
Next

' The following statement generates a COMPILER ERROR.
Return AllOnes()
End Function


Function AllOnes(n As Integer) As Integer()
Dim iArray(n - 1) As Integer
For i = 0 To n - 1
iArray(i) = 1
Next
Return iArray
End Function


## 程序呼叫未修改引數Argument not modified by procedure call

• 基礎變數Underlying variable. 若要允許程式取代基礎變數元素本身的值，程式必須宣告參數ByRefTo allow the procedure to replace the value of the underlying variable element itself, the procedure must declare the parameter ByRef. 此外，呼叫程式碼不能將引數括在括弧中，因為這會覆寫 ByRef 傳遞機制。Also, the calling code must not enclose the argument in parentheses, because that would override the ByRef passing mechanism.

• 參考型別元素Reference type elements. 如果您宣告了參數ByVal，程式就無法修改基礎變數元素本身。If you declare a parameter ByVal, the procedure cannot modify the underlying variable element itself. 不過，如果引數是參考型別，則程式可以修改它所指向之物件的成員，即使它無法取代變數的值也一樣。However, if the argument is a reference type, the procedure can modify the members of the object to which it points, even though it cannot replace the variable's value. 例如，如果引數是陣列變數，則程式無法指派新的陣列給它，但它可以變更一個或多個其元素。For example, if the argument is an array variable, the procedure cannot assign a new array to it, but it can change one or more of its elements. 已變更的元素會反映在呼叫程式碼的基礎陣列變數中。The changed elements are reflected in the underlying array variable in the calling code.

Public Sub increase(ByVal a() As Long)
For j As Integer = 0 To UBound(a)
a(j) = a(j) + 1
Next j
End Sub

Public Sub replace(ByVal a() As Long)
Dim k() As Long = {100, 200, 300}
a = k
For j As Integer = 0 To UBound(a)
a(j) = a(j) + 1
Next j
End Sub


Dim n() As Long = {10, 20, 30, 40}
Call increase(n)
MsgBox("After increase(n): " & CStr(n(0)) & ", " &
CStr(n(1)) & ", " & CStr(n(2)) & ", " & CStr(n(3)))
Call replace(n)
MsgBox("After replace(n): " & CStr(n(0)) & ", " &
CStr(n(1)) & ", " & CStr(n(2)) & ", " & CStr(n(3)))


Public Sub replace(ByRef a() As Long)


## 無法定義多載Unable to define an overload

• 程式修飾詞關鍵字，例如 PublicSharedStaticProcedure modifier keywords, such as Public, Shared, and Static.
• 參數名稱。Parameter names.
• 參數修飾詞關鍵字，例如 ByRefOptionalParameter modifier keywords, such as ByRef and Optional.
• 傳回值的資料類型（轉換運算子除外）。The data type of the return value (except for a conversion operator).

## 呼叫錯誤的多載程式版本Calling the wrong version of an overloaded procedure

• 請以正確的順序提供正確的引數數目。Supply the correct number of arguments, and in the correct order.
• 在理想的情況下，您的引數應該具有與對應參數完全相同的資料類型。Ideally, your arguments should have the exact same data types as the corresponding parameters. 在任何情況下，每個引數的資料型別都必須擴展為其對應參數的資料類型。In any case, the data type of each argument must widen to that of its corresponding parameter. 即使Option Strict 語句設定為 Off也是如此。This is true even with the Option Strict Statement set to Off. 如果多載需要引數清單的任何縮小轉換，該多載就不符合呼叫的資格。If an overload requires any narrowing conversion from your argument list, that overload is not eligible to be called.
• 如果您提供需要擴展的引數，請將其資料類型盡可能靠近對應的參數資料類型。If you supply arguments that require widening, make their data types as close as possible to the corresponding parameter data types. 如果有兩個以上的多載接受您的引數資料類型，則編譯器會解析呼叫最少量擴展的多載呼叫。If two or more overloads accept your argument data types, the compiler resolves your call to the overload that calls for the least amount of widening.

### 多載解析失敗Overload resolution failure

Overloads Sub z(ByVal x As Byte, ByVal y As Double)
End Sub
Overloads Sub z(ByVal x As Short, ByVal y As Single)
End Sub
Overloads Sub z(ByVal x As Integer, ByVal y As Single)
End Sub

Dim r, s As Short
Call z(r, s)
Dim p As Byte, q As Short
' The following statement causes an overload resolution error.
Call z(p, q)


Call z(CType(p, Short), CType(q, Single))