views:

64

answers:

4

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.

A: 

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

StingyJack
+4  A: 

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
Fredou
If I'm not mistaken, I think this will only work in .net 3.5. I'm using 2.0.
Aaron Sanders
seem to work under 2.0
Fredou
generics work in 2.0
Joel Coehoorn
Ah, didn't realize that worked in 2.0. Thanks, guys.
Aaron Sanders
+1  A: 

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.

Matt
A: 

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
Dennis
Using an interface is what I was aiming at with my above comment about the code of the SubscribeObject and ProvideObject....
Dan Appleyard