【问题标题】:Implicit conversion warning in VB.NetVB.Net 中的隐式转换警告
【发布时间】:2017-08-25 08:04:17
【问题描述】:

首先,我的代码如下:

    Dim eventType As Object = Nothing

    eventType = GetEventType()

    If Not (eventType Is Nothing) Then

        If TypeOf eventType Is ClassSelectEvents Then
            m_selectEvents = eventType  ' Warning BC 42016: Implicit type conversion from 'Object' to 'ClassSelectEvents'.
        End If

        If TypeOf eventType Is ClassMouseEvents Then
            m_mouseEvents = eventType   ' Warning BC 42016: Implicit type conversion from 'Object' to 'ClassMouseEvents'.
        End If

        If TypeOf eventType Is ClassTriadEvents Then
            m_triadEvents = eventType   ' Warning BC 42016: Implicit type conversion from 'Object' to 'ClassTriadEvents'.
        End If

    End If

由于编译后显示警告,我修改如下图,但还是显示警告。

在第二个 If 语句中,我认为 eventType 的类型是 Object。那有什么不同吗?我的代码哪里错了请告诉我如何隐藏警告?

提前致谢。

    Dim eventType As Object = Nothing

    eventType = GetEventType()

    If Not (eventType Is Nothing) Then

        If TypeOf eventType Is ClassSelectEvents Then
            'm_selectEvents = eventType
            'm_selectEvents = TryCast(eventType, ClassSelectEvents)
            m_selectEvents = DirectCast(eventType, ClassSelectEvents)
        End If

        If TypeOf eventType Is ClassMouseEvents Then
            'm_mouseEvents = eventType
            'm_selectEvents = TryCast(eventType, ClassMouseEvents)  ' Warning BC42016: Implicit type conversion from 'ClassMouseEvents' to 'ClassSelectEvents'.
            m_selectEvents = DirectCast(eventType, ClassMouseEvents)    ' Warning BC42016: Implicit type conversion from 'ClassMouseEvents' to 'ClassSelectEvents'.
        End If

        If TypeOf eventType Is ClassTriadEvents Then
            'm_triadEvents = eventType
            'm_selectEvents = TryCast(eventType, ClassTriadEvents)  ' Warning BC42016: Implicit type conversion from 'ClassTriadEvents' to 'ClassSelectEvents'.
            m_selectEvents = DirectCast(eventType, ClassTriadEvents)    ' Warning BC42016: Implicit type conversion from 'ClassTriadEvents' to 'ClassSelectEvents'.
        End If

    End If

【问题讨论】:

    标签: vb.net object compiler-warnings implicit-conversion


    【解决方案1】:

    当最后两个应该是 m_mouseEventsm_triadEvents 时,您将在所有三个 If 块中分配给 m_selectEvents

    顺便说一句,在那里使用TryCast 是没有意义的,因为您的If 声明已经保证强制转换会起作用。你应该只使用DirectCast。如果你想使用TryCast,那么你可以这样做:

    m_selectEvents = TryCast(eventType, ClassSelectEvents)
    
    If m_selectEvents Is Nothing Then
        m_mouseEvents = DirectCast(eventType, ClassMouseEvents)
    
        If m_mouseEvents Is Nothing Then
            m_triadEvents = DirectCast(eventType, ClassTriadEvents)
        End If
    End If
    

    TryCast 将在转换失败时返回 Nothing,因此您在尝试转换后测试 Nothing。如果您在使用TryCast 之后没有测试Nothing,那么您几乎肯定不应该从一开始就使用TryCast。 编辑:嗯...我看到您在我发布答案之后/同时将TryCast 更改为DirectCast。希望我的解释对一些人有所帮助。

    【讨论】:

    • 感谢您非常干净且易于理解的回答。我不是 VB.Net 开发人员。一个半小时前,我才刚刚开始浏览代码。所以,我不知道 TryCast、Ctype、DirectCast 之间的区别。我现在正在调查。
    猜你喜欢
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多