Trouble Implementing my Interface

vendiddy

Well-known member
Joined
May 14, 2006
Messages
64
Programming Experience
1-3
Hi all. I have a class like so:
VB.NET:
Public Class Graph
    Public Interface Node
        Property ID() As Integer
        Property Location() As Point
    End Interface
    Public Interface Edge
        Property ID() As Integer
        Property StartNode() As Node
        Property EndNode() As Node
    End Interface

    Public Nodes() As Node
    Public Edges() As Edge
End Class
And I declare a class like this:

VB.NET:
Public Class GEdge
    Inherits GraphicsItem
    Implements Graph.Edge

    Private _StartNode As GNode
    Public Property StartNode() As GNode Implements Graph.Edge.StartNode
        Get
            Return _StartNode
        End Get
        Set(ByVal value As GNode)
            _StartNode = value
        End Set
    End Property

    Private _EndNode As GNode
    Public Property EndNode() As GNode implements Graph.Edge.EndNode
        Get
            Return _EndNode
        End Get
        Set(ByVal value As GNode)
            _EndNode = value
        End Set
    End Property

    Private _ID As Integer
    Public Property ID() As Integer Implements Graph.Edge.ID
        Get
            Return _ID
        End Get
        Set(ByVal value As Integer)
            _ID = value
        End Set
    End Property

End Class

I also have a class called GNode which correctly implements Graph.Node.

But I get these errors:
'StartNode' cannot implement 'StartNode' because there is no matching property on interface 'Graph.Edge'.
'EndNode' cannot implement 'EndNode' because there is no matching property on interface 'Graph.Edge'.

Why am I not allowed to do this? Can't GNode act as a Node? :confused:

If what I'm doing is not possible, is there a good workaround?

Thanks. :)
 
It's because Graph.Edge.EndNode is defined as Graph.Node and not GNode. You can still store a GNode instance there since that is an implementation of Graph.Node.

Intellisense will help you write this stuff, when you press Enter key after writing "Implements Graph.Edge" all implementations of the interface is automatically generated.

The naming recommendation for interfaces is to prefix the name with "I", like "INode" and "IEdge".
 
I purposely made the property have a type of GNode because I thought GNode implements Node. I thought the compiler would agree because TypeOf GNode Is Node.

Thanks. If I have a class declared like this:

VB.NET:
Dim MyEdge as new GEdge()

I would like MyEdge.StartNode to have a return type of GNode. But I would also want DirectCast(MyEdge, Edge).StartNode to have a return type of Node. Guess this isn't possible (i.e., I must do some casting) huh?

Thanks.
 
Back
Top