tags:

views:

47

answers:

2

I just discovered, quite by accident, that this seems to work:

Public Interface Ix
    ReadOnly Property TestProp()
End Interface

Public Interface Iy
    Property TestProp()
End Interface

Public Sub TestSub
    Dim x As Ix = Me.InstantiationNotImportant()
    Dim y As Iy = CType(x, Iy)
End Sub

Maybe I've written a bit too much code today, but this doesn't make sense to me. How is it possible that I can convert one interface to another one that is even slightly different?

Any thoughts?

EDIT: It appears that I was indeed just light-headed from too much coding. After a little sleep, the world makes sense again. :)

+1  A: 

Instantiation is important as that method could produce an object that implements both interfaces making this code make complete sense. Otherwise, I'm not sure.

Garry Shutler
+1  A: 

Like Garry said Instantiation is important because if I do this:

public interface IX { }
public interface IY { }
public class Test : IX { }

The following won't work.

IX xvar = new Test();
IY yvar = xvar as IY; \\Returns null.

IY yvar = (IY)xvar; \\Throws cast exception.

it runs but yvar will be null because the cast is not possible, but if you declare test like this:

public class Test : IX,IY { }

It will now work, as Test uses both interfaces and casting from the object in xvar to Y is valid.

Nathan W