How do I make objects implement both interfaces

  • Thread starter Thread starter Phillip Taylor
  • Start date Start date
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.
 
You could likely have interface TableObject inheriting from interface
Lockable :

Interface TableObject
Inherits Lockable
' Add TableObject specifics here
End Interface
 
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
 
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.
 
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.
 
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.
 
Back
Top