Why I don't like Visual Basic.NET – Part #1

In my work, I have to work quite often with Visual Basic. This language have some advantages over C#, but most of the time, it is very annoying.

Let’s take Interfaces for example:

Public Interface IDoNothing
    Sub Method()
End Interface

And we can implement two classes:

Public Class FirstClass
    Implements IDoNothing

    Public Sub Method()
    End Sub

    Public Sub ThisIsMethod() Implements IDoNothing.Method
    End Sub
End Class

Public Class PrivatClass
    Implements IDoNothing

    Private Sub Method() Implements IDoNothing.Method
    End Sub
End Class

So if you implement interface, your method could have any name you want, but it is enough that there is ‚implements’ keyword (which is obligatory).
Also it doesn’t matter if you make your method private. You can still access it from outside if it implements something. Very obious.

So here are examples of use:

Public Class MainClass
    Public Shared Sub Main()
        Console.WriteLine("As Interface:")
        Dim asInterface As IDoNothing
        asInterface = New FirstClass
        asInterface.Method() 'Outputs: FirstClass.ThisIsMethod (as everyone would expect)

        Console.WriteLine("As Class:")
        Dim asClass As FirstClass
        asClass = New FirstClass
        asClass.Method() 'Outputs: FirstClass.Method
        asClass.ThisIsMethod() 'Outputs: FirstClass.ThisIsMethod

        Console.WriteLine("As Private Class:")
        Dim asPrivateClass As IDoNothing
        asPrivateClass = New PrivatClass
        asPrivateClass.Method() 'Outputs: PrivatClass.Method (Mom, I've just called private method without reflection!!!!111one)

    End Sub
End Class

I think I will start use some random names for methods that implements interfaces in my work. Just for fun }:>

Jedno przemyślenie nt. „Why I don't like Visual Basic.NET – Part #1”

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *