0

The first, my code as below:

    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

Because of the warning display after the compiler, I modified it as shown below but still displayed a warning.

At the second If statement, i think type of eventType is Object. Is that different? My code where is wrong Please tell me how hide warnings ?

Thanks in advance.

    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
Oosutsuke
  • 79
  • 3
  • 12

1 Answers1

1

You are assigning to m_selectEvents in all three If blocks when the last two should be m_mouseEvents and m_triadEvents.

By the way, there's no point using TryCast there because your If statement has already guaranteed that the cast will work. You should just use DirectCast. If you wanted to use TryCast then you'd do it like this:

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 will return Nothing if the cast fails so you test for Nothing after trying to cast. If you're not testing for Nothing after using TryCast then you almost certainly shouldn't be using TryCast to begin with. EDIT: Hmmm... I see that you changed TryCast to DirectCast after/while I posted my answer. Hopefully my explanation helped some anyway.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Thank you for your very clean and easy-to-understand answer. I'm not a VB.Net developer. I've literally just started skimming the code an hour and a half ago. So, I don't know the differences between TryCast, Ctype, DirectCast. I'm looking into it now. – Oosutsuke Mar 31 '17 at 07:14