2011-02-25 27 views
1

我知道System.Nullable(Of T)結構的存在。爲什麼調用Ctype在運行時失敗,從int轉換爲泛型?

我想寫一個類來取代它:Nullable(Of T)類[VB代碼下面]。但是,當我測試Nullable(Of T)類(請參閱NullableOfTClassTest類,Method Main)時,使用CType運算符從System.Int32進行轉換時會發生異常。

爲什麼在運行時發生這種情況,爲什麼我的Ctype操作符方法不被調用?

注意:代碼下面重新排序和減少爲了hilite問題。

Namespace MyNamespace 
Public NotInheritable Class NullableOfTClassTest 
      Private Sub New() 
      End Sub 

      Public Shared Sub Main() 
     Dim dic as new Dictionary(Of String, Object) FROM {{"5", 5}} 
     Dim Y as MyNamespace.Nullable(Of System.Int32) = 
      CType(dic.Item("5"), MyNamespace.Nullable(Of Integer)) 
      End Sub 
    End Class 

Public NotInheritable Class Nullable(Of T As {Structure}) 
    'I think this operator should be called by the code above 
    Public Shared Widening Operator CType(ByVal x As T) As  
                      MyNamespace.Nullable(Of T) 
     Return New MyNamespace.Nullable(Of T)(x) 
    End Operator 

    Public Shared Narrowing Operator CType(ByVal x As  
                             MyNamespace.Nullable(Of T)) As T 
     If (x Is Nothing) OrElse (Not x.HasValue) Then Throw New InvalidCastException 
          Return x.Value 
    End Operator 

    Private _Value As T 
    Private _HasValue As Boolean = False 

    Public Sub New() 
      End Sub 

      Public Sub New(ByVal value As T) 
        Me.Value = value 
    End Sub   

    Public Property Value As T 
       Get 
         If Not Me.HasValue Then Throw New InvalidOperationException(
              "El objeto que acepta valores Null debe tener un valor.") 
       Return Me._Value 
     End Get 
        Set(ByVal value As T) 
            Me._Value = value 
            Me._HasValue = True 
        End Set 
    End Property 

    Public ReadOnly Property HasValue As Boolean 
      Get 
         Return Me._HasValue 
      End Get 
    End Property 

    Public Shadows Function ToString() As String 
      If Me.HasValue Then 
          Return Me.Value.ToString 
      Else 
          Return Nothing 
       End If 
    End Function 

    End Class 


End Namespace 
+0

這是什麼問題?這是一個問題和解答網站。 – Greg

+2

- 平等比較工作正常; ==使用「取消」比較規則,並將*做正確的事情* - 拳擊工程絕對好;空框爲空,否則該值被裝箱;沒有瘋狂;同樣拆開「T」或「T」按預期工作 - 重新開課;這裏要說的一點就是保留值類型的行爲012b- re DbNull - 這不是一個語言概念;爲什麼要有特殊的待遇?你會驚訝於我不會使用'DbNull'的頻率... 然而,重新DbNull你可以通過擴展方法來做到這一點。可以肯定的是,對於大多數人來說它是過度殺傷性的。 –

+2

現在...有沒有真正的問題?這是不是顯示和告訴... –

回答

0

也許你需要看看如何,例如,linq to sql處理Nothing和dbnull並複製它的功能?使用反射

什麼你現在在做什麼的......其實,我甚至不真正瞭解它

+0

感謝Fredou用於讀取和有價值的和相互尊重的(一些神一樣的聲譽成員缺少的東西)的答案。讀一點關於LINQ擴展你建議我已經打開了我的想法一個新的方式(再次感謝)。 LINQ肯定會成爲我用數據庫值進行威脅的方式。但我的問題仍在空中。爲什麼(在我公開的代碼中)運行時不使用定義的(在Nullable(Of T)類代碼中)從DBNull和T類型轉換? –

0

這是否說明要解決的問題?:

Dim o as Object = Nothing 

Dim v as System.Nullable(Of Integer) 

v = 123 

o = v 
' o is now a boxed Integer, and 
' NOT a boxed System.Nullable(Of Integer) 
' where Value is 123 (and HasValue is True) 

v = Nothing 
' Hence, v.HasValue = False 

o = v 
' o is now Nothing, and 
' NOT a boxed System.Nullable(Of Integer) 
' where HasValue is False 

如果是這樣,想想爲什麼你需要做一個盒裝的System.Nullable類型。有更好的方法來剝皮DBNull貓。

相關問題