Aaron Sanders
Aaron Sanders

Reputation: 726

Similar classes with different signatures

I have two classes:


    Public Class Subscribing

    Private _subscribingObjects As IList(Of String)

    Public Sub Add(ByVal obj As SubscribeObject)
        '...code...'
    End Sub

    Public Sub Remove(ByVal index As Integer)
        '...code...'
    End Sub

    End Class

    Public Class Providing

    Private _providingObjects As IList(Of String)

    Public Sub Add(ByVal obj As ProvideObject)
        '...code...'
    End Sub

    Public Sub Remove(ByVal index As Integer)
        '...code...'
    End Sub

    End Class

Is there a more elegant way to add do this? One class would suffice, but since the Add methods have different arguments, then one really wouldn't work.

Any help would be appreciated.

Upvotes: 0

Views: 148

Answers (4)

StingyJack
StingyJack

Reputation: 19459

Eh.. probably not. They are different enough that you can not even Interface them.

Upvotes: 0

Dennis Traub
Dennis Traub

Reputation: 51624

I personally wouldn't mix the two responsibilities (of subscribing and providing) in one class. The classes themselves can easily be simplified by just inheriting from List(Of T)

Public Class Subscribing
    Inherits List(Of SubscribeObject)
End Class

Public Class Providing
    Inherits List(Of ProvideObject)
End Class

If you really want to get down to one class and make sure that it can only accept SubscribeObject and ProvideObject respectively, implement a common interface in both SubscribeObject and ProvideObject. Then create a generic class that accepts the interface:

' Common interface '
Public Interface ISubscribeProvideObject
End Interface

' SubscribeObject and ProvideObject both implementing the common interface '
Public Class SubscribeObject
    Implements ISubscribeProvideObject
    '...'
End Class

Public Class ProvideObject
    Implements ISubscribeProvideObject
    '...'
End Class

' Generic class accepting both types '
Public Class SubscribingProviding(Of T As ISubscribeProvideObject)
    Inherits List(Of T)
    '... Add() and Remove() methods only needed if custom logic applies ...'
End Class

Upvotes: 0

Matt
Matt

Reputation: 2088

Your add functions should be fine. As long as you have different variable types being passed in you can have the function names be the same. Your remove Subs will not be allowed in the same class because it is using the same parameter Integer.

Upvotes: 1

Fredou
Fredou

Reputation: 20090

this?

Public Class SubscribingProviding(Of t)

Private _subscribingObjects As IList(Of String)

Public Sub Add(ByVal obj As t)
    '...code...'
End Sub

Public Sub Remove(ByVal index As Integer)
    '...code...'
End Sub

End Class

Upvotes: 4

Related Questions