views:

300

answers:

1

Well its simple here you have my vb.net code:

Public Class Class1
    Public Overridable ReadOnly Property Name() As String
        Get
            Return Nothing
        End Get
    End Property
End Class

Public Class Class2
    Inherits Class1
    Public Overloads ReadOnly Property Name() As String
        Get
            Return "Class2"
        End Get
    End Property
End Class

Module Module1
    Sub Main()
        Dim c2 As New Class2()
        Console.WriteLine(c2.Name)
        Dim c1 As Class1 = CType(c2, Class1)
        Console.WriteLine(c1.Name)
    End Sub
End Module

And here comes the c# code:

class Class1
{
    public virtual string Name
    {
        get
        {
            return null;
        }
    }
}

class Class2 : Class1
{
    public override string Name
    {
        get
        {
            return "Class2";
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Class2 c2 = new Class2();
        Console.WriteLine(c2.Name);
        Class1 c1 = (Class1)c2;
        Console.WriteLine(c1.Name);
    }
}

i did expekt them to do the same thing but guess what thay dont! C# Output

Class2

Class2

VB.NET output

Class2

{Nothing}

(It dosent print nothing it just prints an empty line) Why does vb.net go for the base class implementation when its overriden?

+2  A: 

Should it be overrides and not overloads?

Public Overrides ReadOnly Property Name() As String
Rippo
VB.NET: Overrides
John K
Yes, its overrides, overloads is to add more parameters.
SoMoS
This is clearly the correct answer, but why doesn't the compiler give an error?
MarkJ
Because overload is valid! I would google overload versus override for vb.net, there are quite a few good explanations.
Rippo