【发布时间】: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