Rob Sobers
Rob Sobers

Reputation: 21135

How can I make an interface property optionally read-only in VB.NET?

This is a follow-up to a previous question I had about interfaces. I received an answer that I like, but I'm not sure how to implement it in VB.NET.

Previous question:

Should this property be part of my object's interface?

public interface Foo{
  bool MyMinimallyReadOnlyPropertyThatCanAlsoBeReadWrite {get;}
}

How can I achieve this with the VB.NET syntax? As far as I know, my only option is to mark the property as ReadOnly (I cannot implement the setter) or not (I must implement the setter).

Upvotes: 2

Views: 3692

Answers (2)

Chris Marasti-Georg
Chris Marasti-Georg

Reputation: 34650

Simply define the getter in one interface, and create a second interface that has both the getter and the setter. If your concrete class is mutable, have it implement the second interface. In your code that deals with the class, check to see that it is an instance of the second interface, cast if so, then call the setter.

Upvotes: 2

Martin Moser
Martin Moser

Reputation: 580

In VB.NET I would implement it this way:

Public Interface ICanBeSecure

    ReadOnly Property IsSecureConnection() As Boolean
End Interface

Public Interface IIsSecureable
    Inherits ICanBeSecure

    Shadows Property IsSecureConnection() As Boolean
End Interface

Upvotes: 1

Related Questions