views:

1150

answers:

2

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:

http://stackoverflow.com/questions/239909/should-this-property-be-part-of-my-objects-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).

+2  A: 

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.

Chris Marasti-Georg
This doesn't work: since you'd have to implement the interface twice in that case, read more here: http://stackoverflow.com/questions/453218/interface-inheritance/453265#453265
Patrik Hägne
It does work, you just need to downcast the object to the the Settable interface in order for it to work.
STW
+1  A: 

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
Martin Moser