EDN Admin
Well-known member
Hi. I was giving a look at polimorphism, late binding and so on. I do not understand why, in this simple fragment of code, the compiler does not complain, even if I get an error at run time:
Option Strict On
Module Module1
Sub Main()
Dim A = New Animal()
Dim P As Printable
P = CType(A, Printable)
End Sub
Public Class Animal
End Class
Interface Printable
Sub Print()
End Interface
End Module
I mean, if I am not wrong, in order to have late binding without using Reflection, I have to setOption Strict Off and to use a variable of type Object. Well, in that fragment of code I setOption Strict On and I am using a variable of type Printable. So, why does not the compiler alert me that the typeAnimal does not implement the interface Printable? It seems to me that the compiler prepares itself to use late binding, but it should know that this is not the case. Ok, at run time I get anInvalidCastException, but why waiting run time when I could realize that at compile time? Thanks.
View the full article
Option Strict On
Module Module1
Sub Main()
Dim A = New Animal()
Dim P As Printable
P = CType(A, Printable)
End Sub
Public Class Animal
End Class
Interface Printable
Sub Print()
End Interface
End Module
I mean, if I am not wrong, in order to have late binding without using Reflection, I have to setOption Strict Off and to use a variable of type Object. Well, in that fragment of code I setOption Strict On and I am using a variable of type Printable. So, why does not the compiler alert me that the typeAnimal does not implement the interface Printable? It seems to me that the compiler prepares itself to use late binding, but it should know that this is not the case. Ok, at run time I get anInvalidCastException, but why waiting run time when I could realize that at compile time? Thanks.
View the full article