.NET: VB.net Getter Only Interface Interface Problem

Why interface overrides class definition and breaks class encapsulation? I have included two examples below: one in C # and one in VB.net ?

Vb.net

Module Module1

    Sub Main()
        Dim testInterface As ITest = New TestMe
        Console.WriteLine(testInterface.Testable) ''// Prints False
        testInterface.Testable = True             ''// Access to Private!!!
        Console.WriteLine(testInterface.Testable) ''// Prints True

        Dim testClass As TestMe = New TestMe
        Console.WriteLine(testClass.Testable)     ''// Prints False
        ''//testClass.Testable = True             ''// Compile Error
        Console.WriteLine(testClass.Testable)     ''// Prints False
    End Sub

End Module

Public Class TestMe : Implements ITest
    Private m_testable As Boolean = False
    Public Property Testable As Boolean Implements ITest.Testable
        Get
            Return m_testable
        End Get
        Private Set(ByVal value As Boolean)
            m_testable = value
        End Set
    End Property
End Class

Interface ITest

    Property Testable As Boolean

End Interface

WITH#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterfaceCSTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ITest testInterface = new TestMe();
            Console.WriteLine(testInterface.Testable);
            testInterface.Testable = true;
            Console.WriteLine(testInterface.Testable);

            TestMe testClass = new TestMe();
            Console.WriteLine(testClass.Testable);
            //testClass.Testable = true;
            Console.WriteLine(testClass.Testable);
        }
    }

    class TestMe : ITest
    {
        private bool m_testable = false;
        public bool Testable
        {
            get
            {
                return m_testable;
            }
            private set
            {
                m_testable = value;
            }
        }
    }

    interface ITest
    {
        bool Testable { get; set; }
    }
}

More specific

How to implement an interface in VB.net , which allows you to use a private setter . For example, in C #, I can declare:

class TestMe : ITest
{
    private bool m_testable = false;
    public bool Testable
    {
        get
        {
            return m_testable;
        }
        private set //No Compile Error here!
        {
            m_testable = value;
        }
    }
}

interface ITest
{
    bool Testable { get; }
}

However, if I declare the interface property as readonly in VB.net , I cannot create a setter. If I create a VB.net interface as just an old property, .

Public Class TestMe : Implements ITest
    Private m_testable As Boolean = False
    Public ReadOnly Property Testable As Boolean Implements ITest.Testable
        Get
            Return m_testable
        End Get
        Private Set(ByVal value As Boolean) ''//Compile Error
            m_testable = value
        End Set
    End Property
End Class

Interface ITest

    ReadOnly Property Testable As Boolean

End Interface

, , getter only Interface VB.net ?

, . , , . getter (Readonly), #, VB.net. , ?

Update

Hans Passant, : https://connect.microsoft.com/VisualStudio/feedback/details/635591/create-a-readonly-interface-that-allows-private-setters-c-to-vb-net-conversion

, , !

+5
3

VB.NET:

Public Interface ITest
    ReadOnly Property Testable As Boolean
End Interface

Public Class Test
    Implements ITest

    ' Note: Here I am NOT implementing the interface. '
    Private _testable As Boolean
    Public Property Testable() As Boolean
        Get
            Return _testable
        End Get
        Private Set(ByVal value As Boolean)
            _testable = value
        End Set
    End Property

    ' This is where I define a read-only property to satisfy the interface '
    ' (from the perspective of the VB compiler). '
    ' Notice this is a lot like explicit interface implementation in C#. '
    Private ReadOnly Property TestableExplicit() As Boolean Implements ITest.Testable
        Get
            Return Testable
        End Get
    End Property
End Class
+4

, . , " ", "".

+1

, :

?

. ( ) , , :

Interface I
    Sub DoSomething()
End Interface

? , i i, i.DoSomething().

Public Class C
    Implements I
    Private Sub DoSomething() Implements I.DoSomething
        ...
    End Sub
End Class

Implements I.DoSomething, , , C i, I.DoSomething C.DoSomething ( , ). . Implements ..., , , () . , Implements ( ), "" .

This way it DirectCast(myC, I).DoSomething()will work (since it accesses the interface method that happens with the "invocation" of your private method), but myC.DoSomething()will not compile when used outside C.

The same thing happens in your example: An indication Property Testable As Booleanin your interface ensures that obj.Testable = ...you can call (publicly) on each objtype ITest.

0
source

All Articles