共用方式為


Visual Basic 2008 的重大變更

更新: 2008 年 7 月

下列表格列出可能造成在 Visual Studio 2005 中建立的應用程式,無法在 Visual Studio 2008 中進行編譯,或導致執行階段行為改變的變更。

類別

變更

說明

可為 Null 的型別

T 擴展為 T? 在 Visual Studio 2008 中成為預先定義的轉換。

在 Visual Studio 2005 中,您可以建立使用者定義的轉換,讓 T 能夠擴展為 Nullable(Of T)。在 Visual Studio 2008 中這項轉換是預先定義的,包含在對可為 Null 的型別的新增支援中。

因為多載解析 (Overload Resolution) 會同時考慮預先定義與使用者定義的轉換,如果兩者都存在時,可能造成模稜兩可 (Ambiguity) 的情況。因此在 Visual Studio 2008 中,當程式碼包含由 T 轉換至 T? 的使用者定義擴充轉換時,可能會造成模稜兩可的情況。

例如,在下列程式碼中,對 c3.Sub1 的呼叫可以在 Visual Studio 2005 中正常運作,但卻會在 Visual Studio 2008 中造成編譯器錯誤。

Module Module1
Sub Main()
Dim c3 As New Class3
Dim sh As Short = 2
' The following statement compiles in Visual Studio 2005,
' but causes an error in Visual Studio 2008.
'c3.Sub1(sh)
End Sub
End Module
Class Class1
Public Shared Widening Operator CType(ByVal s As _
Nullable(Of Short)) As Class1
Return New Class1()
End Operator
End Class
Class Class2
Public Shared Widening Operator CType(ByVal n As Integer) As Class2
Return New Class2()
End Operator
End Class
Class Class3
Sub Sub1(ByVal c As Class1)
End Sub
Sub Sub1(ByVal c As Class2)
End Sub
End Class

若要解決這個問題,您可以移除使用者定義的轉換,或明確轉型成 Class1 或 Class2:

c3.Sub1(CType(sh, Class1))

多載解析

在泛型與非泛型類別間的多載解析差異已更正。

在 Visual Studio 2005 中有一個問題,會造成泛型類別的多載解析行為和非泛型類別的行為不同。在下列範例中,除了在 Class2 上定義了泛型參數以外,Class1 和 Class2 完全一模一樣。在 Main 中對 c1.Sub1 的呼叫模稜兩可,因為傳遞的引數可以繫結至 Class1 中 Sub1 的任一個多載。這樣模稜兩可的情況,在 Visual Studio 2005 和 Visual Studio 2008 中都會造成編譯器錯誤。

對 c2.Sub1 的呼叫應該要產生一樣的錯誤,但在 Visual Studio 2005 中並不會產生錯誤。相反地,方法會繫結至 Class2 中 Sub1 的不受限制多載。

這個問題在 Visual Studio 2008 中已修正。兩種呼叫現在都會因為模稜兩可而產生編譯器錯誤。這在下列程式碼中說明:

Module Module1
Sub Main()
Dim c1 As New Class1
Dim c2 As New Class2(Of String)
' The following statement causes a compiler error in both
' Visual Studio 2005 and Visual Studio 2008.
'c1.Sub1(New Nullable(Of Integer))
' The following statement causes a compiler error only
' in Visual Studio 2008.
'c2.Sub1(New Nullable(Of Integer))
End Sub
End Module
Class Class1
Sub Sub1(Of T)(ByVal arg As T)
Console.WriteLine("Unconstrained Sub1 in Class1")
End Sub
Sub Sub1(Of T As Structure)(ByVal arg As Nullable(Of T))
Console.WriteLine("Constrained Sub1 in Class1")
End Sub
End Class
Class Class2(Of U)
Sub Sub1(Of T)(ByVal arg As T)
Console.WriteLine("Unconstrained Sub1 in Class2")
End Sub
Sub Sub1(Of T As Structure)(ByVal arg As Nullable(Of T))
Console.WriteLine("Constrained Sub1 in Class2")
End Sub
End Class

若要解決這個問題,您可以變更多載而不再模棱兩可,或者明確指定型別引數:

c1.Sub1(Of Integer?)(New Nullable(Of Integer))
c2.Sub1(Of Integer?)(New Nullable(Of Integer))

多載解析

使用泛型和 ParamArray 參數的多載解析,在 Visual Studio 2008 中會產生不一樣的結果。

多載解析的目的,是嘗試選取其參數與傳遞給方法的引數型別最緊密符合的候選方法。在本節的範例中,Sub1 跨繼承階層架構多載,並受到型別 Integer 和 Short 的引數呼叫。

在 Visual Studio 2005 和 Visual Studio 2008 中的多載解析規則,都指出直接相符優於需要 ParamArray 參數的相符情況。然而,使用 Visual Studio 2005 多載解析規則時,在衍生類別 Class2 中之候選多載的型別推論會失敗,因為 Y 不能同時為 Integer 和 Short,而且需要完全相符。如果引數 sh 和 n 型別相同,會偏向使用 Class2 中的 Sub1,而不會使用在 Class1 中,具有 ParamArray 參數的候選項目。不過,因為兩項引數的型別不同,因此反而會選取 Class1 中的基底類別多載。T 會推論為 Integer,而 Short 會擴展為 ParamArrayp。

Visual Studio 2008 使用新的演算法,會和 Visual Studio 2005 選取一樣的多載,只有在這個特別案例中不同。在 Visual Studio 2008 中,演算法會將 Integer 決定為本範例中的主型別,因為 Short 會擴展至 Integer。衍生類別中的型別參數 Y 會推論為 Integer,且 Short 會擴展至 Integer,因此會呼叫衍生類別中的 Sub1。

Module Module1
Sub Main()
Dim c2 As New Class2
Dim n As Integer
Dim sh As Short
c2.Sub1(n, sh)
End Sub
Class Class1
Sub Sub1(Of T)(ByVal arg1 As T, ByVal ParamArray p() As Integer)
Console.WriteLine("Visual Studio 2005 calls Sub1 " & _
"in the *base* class.")
End Sub
End Class
Class Class2 : Inherits Class1
Overloads Sub Sub1(Of Y)(ByVal arg1 As Y, ByVal arg2 As Y)
Console.WriteLine("Visual Studio 2008 calls Sub1 " & _
"in the *derived* class.")
End Sub
End Class
End Module

您可以在呼叫 Sub1 前將變數 c2 轉型為型別 C1ass1,或傳遞參數 sh 做為陣列,強迫指定 Visual Studio 2005 的行為。

' You can cast variable c2 to Class1 in order to force Visual
' Studio 2005 behavior.
CType(c2, Class1).Sub1(n, sh)
' Alternatively, you can pass sh as an array.
c2.Sub1(n, New Integer() {sh})

請參閱

概念

多載解析

可為 Null 的實值型別

Visual Basic 中的泛型型別

參考

ParamArray

變更記錄

日期

記錄

原因

2008 年 7 月

加入主題。

資訊加強。