DirectCast(False,Nullable(Of Boolean))错误

时间:2011-04-08 13:13:18

标签: vb.net boolean nullable directcast

我从C#项目中获取了一些代码并将其放入转换器中。原始代码是:

(Nullable<bool>)false

并且转换器说VB等价物是:

DirectCast(False, Nullable(Of Boolean))

我甚至编译了C#项目并在Reflector中查看了它。它给出了与上面相同的VB代码,但这会产生错误:

Value of type 'Boolean' cannot be converted to 'Boolean?'

如何正确投射?

根据要求提供更多代码:

Imports System.Windows
Imports System.Windows.Controls.Primitives
Imports System.Windows.Input

Public Class VirtualToggleButton

    Public Shared ReadOnly IsCheckedProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsChecked", _
            GetType(Nullable(Of Boolean)), _
            GetType(VirtualToggleButton), _
            New FrameworkPropertyMetadata(DirectCast(False, Nullable(Of Boolean)), _
             FrameworkPropertyMetadataOptions.BindsTwoWayByDefault Or _
             FrameworkPropertyMetadataOptions.Journal, _
             New PropertyChangedCallback(AddressOf OnIsCheckedChanged)))

            Public Shared Function GetIsChecked(ByVal d As DependencyObject) As Nullable(Of Boolean)
        Return DirectCast(d.GetValue(IsCheckedProperty), Nullable(Of Boolean))
    End Function

            Public Shared Sub SetIsChecked(ByVal d As DependencyObject, ByVal value As Nullable(Of Boolean))
        d.SetValue(IsCheckedProperty, value)
    End Sub

            Private Shared Sub OnIsCheckedChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim pseudobutton As UIElement = TryCast(d, UIElement)
        If pseudobutton IsNot Nothing Then
            Dim newValue As Nullable(Of Boolean) = DirectCast(e.NewValue, Nullable(Of Boolean))
            If newValue = True Then
                RaiseCheckedEvent(pseudobutton)
            ElseIf newValue = False Then
                RaiseUncheckedEvent(pseudobutton)
            Else
                RaiseIndeterminateEvent(pseudobutton)
            End If
        End If
    End Sub


            Public Shared ReadOnly IsThreeStateProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsThreeState", _
                             GetType(Boolean), _
                             GetType(VirtualToggleButton), _
                             New FrameworkPropertyMetadata(CBool(False)))

            Public Shared Function GetIsThreeState(ByVal d As DependencyObject) As Boolean
        Return CBool(d.GetValue(IsThreeStateProperty))
    End Function

            Public Shared Sub SetIsThreeState(ByVal d As DependencyObject, ByVal value As Boolean)
        d.SetValue(IsThreeStateProperty, value)
    End Sub


            Public Shared ReadOnly IsVirtualToggleButtonProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsVirtualToggleButton", _
                                 GetType(Boolean), _
                                 GetType(VirtualToggleButton), _
                                 New FrameworkPropertyMetadata(CBool(False), _
                                         New PropertyChangedCallback(AddressOf OnIsVirtualToggleButtonChanged)))

            Public Shared Function GetIsVirtualToggleButton(ByVal d As DependencyObject) As Boolean
        Return CBool(d.GetValue(IsVirtualToggleButtonProperty))
    End Function

            Public Shared Sub SetIsVirtualToggleButton(ByVal d As DependencyObject, ByVal value As Boolean)
        d.SetValue(IsVirtualToggleButtonProperty, value)
    End Sub

            Private Shared Sub OnIsVirtualToggleButtonChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim element As IInputElement = TryCast(d, IInputElement)
        If element IsNot Nothing Then
            If CBool(e.NewValue) Then
                AddHandler element.MouseLeftButtonDown, New MouseButtonEventHandler(AddressOf VirtualToggleButton.OnMouseLeftButtonDown)
                AddHandler element.KeyDown, New KeyEventHandler(AddressOf VirtualToggleButton.OnKeyDown)
            Else
                RemoveHandler element.MouseLeftButtonDown, New MouseButtonEventHandler(AddressOf VirtualToggleButton.OnMouseLeftButtonDown)
                RemoveHandler element.KeyDown, New KeyEventHandler(AddressOf VirtualToggleButton.OnKeyDown)
            End If
        End If
    End Sub


            Friend Shared Function RaiseCheckedEvent(ByVal target As UIElement) As RoutedEventArgs
        If target Is Nothing Then
            Return Nothing
        End If

        Dim args As New RoutedEventArgs()
        args.RoutedEvent = ToggleButton.CheckedEvent
        [RaiseEvent](target, args)
        Return args
    End Function

            Friend Shared Function RaiseUncheckedEvent(ByVal target As UIElement) As RoutedEventArgs
        If target Is Nothing Then
            Return Nothing
        End If

        Dim args As New RoutedEventArgs()
        args.RoutedEvent = ToggleButton.UncheckedEvent
        [RaiseEvent](target, args)
        Return args
    End Function

            Friend Shared Function RaiseIndeterminateEvent(ByVal target As UIElement) As RoutedEventArgs
        If target Is Nothing Then
            Return Nothing
        End If

        Dim args As New RoutedEventArgs()
        args.RoutedEvent = ToggleButton.IndeterminateEvent
        [RaiseEvent](target, args)
        Return args
    End Function


    Private Shared Sub OnMouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
        e.Handled = True
        UpdateIsChecked(TryCast(sender, DependencyObject))
    End Sub

    Private Shared Sub OnKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        If e.OriginalSource Is sender Then
            If e.Key = Key.Space Then
                                    If (Keyboard.Modifiers And ModifierKeys.Alt) = ModifierKeys.Alt Then
                    Return
                End If

                UpdateIsChecked(TryCast(sender, DependencyObject))

                e.Handled = True
            ElseIf e.Key = Key.Enter AndAlso CBool(TryCast(sender, DependencyObject).GetValue(KeyboardNavigation.AcceptsReturnProperty)) Then
                UpdateIsChecked(TryCast(sender, DependencyObject))
                e.Handled = True
            End If
        End If
    End Sub

    Private Shared Sub UpdateIsChecked(ByVal d As DependencyObject)
        Dim isChecked As Nullable(Of Boolean) = GetIsChecked(d)
        If isChecked = True Then
            SetIsChecked(d, If(GetIsThreeState(d), DirectCast(Nothing, Nullable(Of Boolean)), DirectCast(False, Nullable(Of Boolean))))
        Else
            SetIsChecked(d, isChecked.HasValue)
        End If
    End Sub

    Private Shared Sub [RaiseEvent](ByVal target As DependencyObject, ByVal args As RoutedEventArgs)
        If TypeOf target Is UIElement Then
            TryCast(target, UIElement).[RaiseEvent](args)
        ElseIf TypeOf target Is ContentElement Then
            TryCast(target, ContentElement).[RaiseEvent](args)
        End If
    End Sub

End Class

2 个答案:

答案 0 :(得分:1)

看起来你可以删除DirectCast。从我看到你将一个假传递给一个有布尔值的函数/方法? (或Nullable(of Boolean))作为参数。 VB不需要C#所需的显式转换(尽管在某些情况下这不是一个坏主意)。举个简单的例子,

Private Function DoSomething(byval param as Boolean?) as Boolean?
   'do something and return a Nullable(of Boolean)
End Function

DoSomething(false) 'is just fine, no DirectCast needed
DoSomething(nothing) 'is also fine
DoSomething(true) 'fine

DoSomething(DirectCast(false, Nullable(of Boolean)) 'will give you the error you described

正如旁注,如果它混淆Nullable(布尔值)和布尔值?意思是同样的事情,它是一个布尔变量,可以有一个值true,false或什么都没有。

答案 1 :(得分:1)

TKTS是正确的,VB通常不需要显式转换,但是,为了完整性,“正确”的转换将是:

New Boolean?(False)