How do I make objects implement both interfaces

P

Phillip Taylor

I have an interface called Lockable and an interface called
TableObject. How do I make sure that any class which implements
Lockable also implements my other, TableObject interface?

Do I have to copy the definition of the TableObject interface into
Lockable? I'd rather not do that incase I need to downcast them to
simple TableObject's later.
 
P

Patrice

You could likely have interface TableObject inheriting from interface
Lockable :

Interface TableObject
Inherits Lockable
' Add TableObject specifics here
End Interface
 
A

Armin Zingler

Phillip Taylor said:
I have an interface called Lockable and an interface called
TableObject. How do I make sure that any class which implements
Lockable also implements my other, TableObject interface?

Do I have to copy the definition of the TableObject interface into
Lockable? I'd rather not do that incase I need to downcast them to
simple TableObject's later.


There is no way to force somebody to implement both interfaces if they are
independent of each other. I'm interested in why you want to do this.(?)

If it is compliant to your interface design, you can also inherit
interfaces:

interface IInterfaceB
inherits IInterfaceA

end interface

Classes that implement IInterfaceB also have to implement IInterfaceA. But,
if you just do this in order to "force" somebody to implement both, but not
because there is really a logical inheritance relation between both
interfaces, which also means that the order of inheritance can be changed (B
inherits from A), I wouldn't do this.


Armin
 
P

Phillip Taylor

There is no way to force somebody to implement both interfaces if they are
independent of each other. I'm interested in why you want to do this.(?)

If it is compliant to your interface design, you can also inherit
interfaces:

interface IInterfaceB
inherits IInterfaceA

end interface

Classes that implement IInterfaceB also have to implement IInterfaceA. But,
if you just do this in order to "force" somebody to implement both, but not
because there is really a logical inheritance relation between both
interfaces, which also means that the order of inheritance can be changed (B
inherits from A), I wouldn't do this.

Armin

There IS a logical and application driven needs to one onto the other.

Take my exact requirements as stated in the original post: I have
objects or type "TableObject" (which signifies this the "Active
Record" approach to development). It makes perfect sense that you can
only implement Lockable objects on actual TableObjects. I mean you
can't lock something which isn't in the database. Hense if someone
implements Lockable, I want to force them to be sure that they've
implemented TableObject first.

Thanks guys.
 
P

Phill W.

Phillip said:
There IS a logical and application driven needs to one onto the other.

Take my exact requirements as stated in the original post: I have
objects or type "TableObject" (which signifies this the "Active
Record" approach to development). It makes perfect sense that you can
only implement Lockable objects on actual TableObjects. I mean you
can't lock something which isn't in the database. Hense if someone
implements Lockable, I want to force them to be sure that they've
implemented TableObject first.

Interface inheritance sounds like the way to go.

Interface ITableObject
Sub X()
Sub Y()
End Interface

Interface ILockableTableObject
Inherits ITableObject
Sub Z()
End Interface

Dim t1 as ITableObject
Dim t2 as ILockableTableObject

Sub HandleTable( t as Object )
If t Is ITableObject Then
With DirectCast( t, ITableObject )
. . .
End With

If t Is ILockableTableObject Then
With DirectCast( t, ILockableTableObject )
. . .
End With
End If
End If
End Sub

HTH,
Phill W.
 
P

Phillip Taylor

Interface inheritance sounds like the way to go.

Interface ITableObject
Sub X()
Sub Y()
End Interface

Interface ILockableTableObject
Inherits ITableObject
Sub Z()
End Interface

Dim t1 as ITableObject
Dim t2 as ILockableTableObject

Sub HandleTable( t as Object )
If t Is ITableObject Then
With DirectCast( t, ITableObject )
. . .
End With

If t Is ILockableTableObject Then
With DirectCast( t, ILockableTableObject )
. . .
End With
End If
End If
End Sub

HTH,
Phill W.

Thanks guys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top