1

Is it possible in vb.net to declare a member variable or property to be some derived class that also happens to implement a interface? By that I mean given:

Class Shape
    ...
End class

Class Cube
    Inherits Shape
    ...
End Class

Class Sphere
    Inherits Shape
    Implements IRollable
    ...
End Class

Class Cylinder
    Inherits Shape
    Implements IRollable
    ...
End Class

Can I declare a Property or "class level variable"(do these have a specific name in vb.net) that is ether a Shape (Sphere or a Cylinder) but not a Cube?

'This is what I want to do
Class A
    Dim roller1 as Shape And IRollable
    'Or
    Property roller2 (Of RollingShape) as {Shape, IRollable}
End Class 

If such a thing is not possible is there an architectural reason why ether in vb.net or the .net framework?

I suppose I could do this empty class and make both the other class inherit it but that seems messy and ugly:

Class RollableShape
    Inherits Shape
    Implements IRollable
End Class
2
  • ISpherical which Implements IRollable which Cube does not implement. I would leave Shape as a Type if there were commonalities shared by Sphere and Cube. RollableShape could inherit from shape and implement ISpherical and by extension, IRollable (which might now be a synonym) Jun 26, 2015 at 1:22
  • According to your own requirements, the RollableShape class idea appears to be what's needed. I guess you'd want to make sure it's an abstract class (MustInherit).
    – sstan
    Jun 26, 2015 at 1:38

1 Answer 1

1

How about defining an IShape interface and having IRollable derive from that?

Interface IShape
...
End Interface

Interface IRollable Inherits IShape
...
End Interface

Class Shape Implements IShape
...
End Class

Class Sphere Inherits Shape Implements IRollable
...
End Class

Class A
  Dim roller As IRollable
End Class

This way A only ever depends on the interfaces like IShape and IRollable.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.