VB.NET: no warning when returning an object that is not an instance of the returned function type

We have this:

Friend NotInheritable Class ConcreteGraphFactory
    Inherits AbstractGraphFactory

    Public Shared ReadOnly Instance As New ConcreteGraphFactory()

    Private Sub New()
        MyBase.New()
    End Sub

    Friend Overrides Function Create() As AbstractGraph
        Return New ConcreteGraph()
    End Function    

    Private NotInheritable Class ConcreteGraph
        Inherits AbstractGraph

        Private ReadOnly Question1 As New Question("Why isn't this showing a warning?")

        Public Overrides Function GetRoot() As IRoot
            Return Question1 '<---HERE
        End Function

        Public Sub New()
        End Sub

    End Class

End Class

And I have IRoot:

Friend Interface IRoot
    Inherits IQuestion
    Function GetContainer() As AbstractGraph
End Interface

And finally Question:

Public Class Question 
    Implements IQuestion

    ' code....

End Class

Why won't VS show a warning? Questiondoes not implement IRoot...

+4
source share
1 answer

If you want the compiler to indicate an error, you need to set the strict to On option. You can do this on the Compilation tab of the Properties project. Or add Option Strict Onto the beginning of the file containing this code.

Here are a few pages that provide more information on what Option Strict means.

http://support.microsoft.com/en-us/kb/311329

https://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx

Strict Off , Visual Basic . , .

, , IRoot , . GetRoot , Question , Strict.

Strict off , COM-. .

, , VB.NET. , , , , . , - VB #, DirectCast, , VB.

C 2009 dynamic 2009 , VB.NET : ". , ". , VB.NET , dynamic #, , VB .

+4

All Articles