Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Interface ReadOnly properties be overridden in VB.NET, when it is valid in C#.NET?

(this is related to this other question)

If you define an Interface where there is a Property with only a getter (= ReadOnly in VB.NET), why can you define the setter in implementing classes with C# but not with VB ?

I would have thought it was defined at .NET level, and not language-specific.

Example: for this interface

'VB.NET
Interface SomeInterface

    'the interface only say that implementers must provide a value for reading
    ReadOnly Property PublicProperty As String

End Interface

or

//C# code
interface IPublicProperty
{
    string PublicProperty { get; }
}

This is a correct implementation in C# :

public class Implementer:IPublicProperty
    {
        private string _publicProperty;

        public string PublicProperty
        {
            get
            {
                return _publicProperty;
            }
            set
            {
                _publicProperty = value;
            }
        }
    }

But this is invalid in VB.NET

Public Property PublicProperty As String Implements SomeInterface.PublicProperty
    Get
        Return _myProperty
    End Get
    Set(ByVal value As String)
        _myProperty = value
    End Set
End Property

UPDATE 2015/04/23

Turns out this feature is coming as part of VB14 ! See Languages features in C# 6 and VB 14 and New Language Features in Visual Basic 14 :

ReadOnly interface properties can be implemented by ReadWrite props This cleans up a quirky corner of the language. Look at this example:

Interface I
    ReadOnly Property P As Integer
End Interface


Class C : Implements I
    Public Property P As Integer Implements I.P
End Class

Previously, if you were implementing the ReadOnly property I.P, then you had to implement it with a ReadOnly property as well. Now that restriction has been relaxed: you can implement it with a read/write property if you want. This example happens to implement it with a read/write autoprop, but you can also use a property with getter and setter.

like image 542
tsimbalar Avatar asked Jun 14 '11 08:06

tsimbalar


1 Answers

Be careful assuming that VB.NET and C# are the same language spoken with a different accent - they're not.

Because VB.NET requires implementation of an interface member to have that Implements clause, saying which member it is implementing. C# lets you implement interface members explicitly (SORT OF like VB.NET), or implicitly (no VB.NET equivalent). Therefore the actual C# version of this is

public class Implementer : IPublicProperty
{
    private string _publicProperty;

    string IPublicProperty.PublicProperty    // explicit implementation
    {
        get
        {
            return _publicProperty;
        }
        set
        {
            _publicProperty = value;
        }
    }
}

and this does gives an error:

error CS0550: 'ConsoleApplication171.Implementer.ConsoleApplication171.IPublicProperty.PublicProperty.set' adds an accessor not found in interface member 'ConsoleApplication171.IPublicProperty.PublicProperty'

like image 152
AakashM Avatar answered Nov 15 '22 18:11

AakashM