none
Create Same Method Name but different Parameter size implement using interface or other way? RRS feed

  • Question

  • I current doing a project with a few class, the method name is same but i wander can possible can have different Parameter size implement using interface or other way?

    Example:

    All the class have method Name

    GetData, Getby, Insert, Update, Delete

    Class A have Method GetData(), GetBy(), Insert(a,b), Update(a,b,c), Delete(a)

    Class B Have Method GetData(), GetBy(ID), Insert(ID, Name, Value), Update(ID,Name, Value), Delete(ID)

    Class C may Have Method GetData(Name), GetBy(ID, Name, Category), Insert(ID, Name, Category, Value), Update(ID, Name, Category, value), Detele(ID)

    Wednesday, March 7, 2018 6:09 AM

All replies

  • I current doing a project with a few class, the method name is same but i wander can possible can have different Parameter size implement using interface or other way?

    If the methods are defined in different classes then they can each have whatever argument list you choose.

    If the methods are defied in the same class then you can have multiple methods with different parameter lists.  See:

    https://www.dotnetheaven.com/article/polymorphism-in-visual-basic-.net
    https://msdn.microsoft.com/en-us/library/ms973896.aspx

    Wednesday, March 7, 2018 8:14 AM
  • Sure, just create a Class with Interface definitions for each signature and then either inherit or just implement them from your other classes.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/interfaces/

    For example, GetBy would have three different Interface definitions, since there are three different signatures:

    Public Interface ISomeInterface
    
        Function GetBy() As ...
        Function GetBy(ByVal ID As String) As ...
        Function GetBy(ByVal ID As String, ByVal Name As String, ByVal Category As String) As ...
    
    End Interface
    
    Public Class A
    
    Public Overloads Function GetBy() As ... Implements ISomeInterface.GetBy
    ...code
    End Function
    
    End Class
    
    Public Class B
    
    Public Overloads Function GetBy(ByVal ID As String) As ... Implements ISomeInterface.GetBy
    ...code
    End Function
    
    End Class
    
    Public Class C
    
    Public Overloads Function GetBy(ByVal ID As String, ByVal Name As String, ByVal Category As String) As ... Implements ISomeInterface.GetBy
    ...code
    End Function
    
    End Class
    
    
    


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, March 7, 2018 4:01 PM
  • The problem with using multiple overloads in an interface is the need to implement all overloads in every class even when some aren't used.  You can specify the member visibility on the class implementation but a cast to the underlying interface type will reveal the methods where were not implemented.  This could be OK if it is understood as part of the design but it could also lead to difficultly when using the interface.

    Another option would be to create the interface methods with a loosely-typed paramarray of object arguments.  Then implement the interface privately and expose strongly typed versions of the interface methods from each class.  The private interface implementations can then call these strongly typed methods.  In this way each class offers its own signature for the methods but all classes can have their specific versions of the methods called from the interface type (assuming a known list of argument types when calling the interface methods).

    For example, you could define an interface like:

    Public Interface ICrudObject
        Function GetData(ParamArray args() As Object) As DataTable
        Function GetBy(ParamArray args() As Object) As DataTable
        Function Insert(ParamArray args() As Object) As Integer
        Function Update(ParamArray args() As Object) As Integer
        Function Delete(ParamArray args() As Object) As Integer
    End Interface

    Then you could have two different classes implement this interface privately, exposing different method signatures in each class:

    'Class A have Method GetData(), GetBy(), Insert(a,b), Update(a,b,c), Delete(a)
    Public Class ClassA
        Implements ICrudObject
    
        Public Function GetData() As DataTable
        End Function
        Public Function GetBy() As DataTable
        End Function
        Public Function Insert(a, b) As Integer
        End Function
        Public Function Update(a, b, c) As Integer
        End Function
        Public Function Delete(a) As Integer
        End Function
    
        Protected Function GetDataInternal(ParamArray args() As Object) As DataTable Implements ICrudObject.GetData
            Return GetData()
        End Function
    
        Protected Function GetByInternal(ParamArray args() As Object) As DataTable Implements ICrudObject.GetBy
            Return GetBy()
        End Function
    
        Protected Function InsertInternal(ParamArray args() As Object) As Integer Implements ICrudObject.Insert
            'TODO:  validate arguments to method signature
            Return Insert(args(0), args(1))
        End Function
    
        Protected Function UpdateInternal(ParamArray args() As Object) As Integer Implements ICrudObject.Update
            'TODO:  validate arguments to method signature
            Return Update(args(0), args(1), args(2))
        End Function
    
        Protected Function DeleteInternal(ParamArray args() As Object) As Integer Implements ICrudObject.Delete
            'TODO:  validate arguments to method signature
            Return Delete(args(0))
        End Function
    End Class
    'Class B Have Method GetData(), GetBy(ID), Insert(ID, Name, Value), Update(ID,Name, Value), Delete(ID)
    Public Class ClassB
        Implements ICrudObject
    
        Public Function GetData() As DataTable
        End Function
        Public Function GetBy(id As Integer) As DataTable
        End Function
        Public Function Insert(id As Integer, name As String, value As Object) As Integer
        End Function
        Public Function Update(id As Integer, name As String, value As Object) As Integer
        End Function
        Public Function Delete(id As Integer) As Integer
        End Function
    
        Protected Function GetDataInternal(ParamArray args() As Object) As DataTable Implements ICrudObject.GetData
            Return GetData()
        End Function
    
        Protected Function GetByInternal(ParamArray args() As Object) As DataTable Implements ICrudObject.GetBy
            If args.Length > 0 AndAlso TypeOf args(0) Is Integer Then
                Return GetBy(CInt(args(0)))
            End If
            Throw New ArgumentException
        End Function
    
        Protected Function InsertInternal(ParamArray args() As Object) As Integer Implements ICrudObject.Insert
            If args.Length > 2 AndAlso TypeOf args(0) Is Integer AndAlso TypeOf args(1) Is String Then
                Return Insert(CInt(args(0)), args(1).ToString, args(2))
            End If
            Throw New ArgumentException
        End Function
    
        Protected Function UpdateInternal(ParamArray args() As Object) As Integer Implements ICrudObject.Update
            If args.Length > 2 AndAlso TypeOf args(0) Is Integer AndAlso TypeOf args(1) Is String Then
                Return Update(CInt(args(0)), args(1).ToString, args(2))
            End If
            Throw New ArgumentException
        End Function
    
        Protected Function DeleteInternal(ParamArray args() As Object) As Integer Implements ICrudObject.Delete
            If args.Length > 0 AndAlso TypeOf args(0) Is Integer Then
                Return Delete(CInt(args(0)))
            End If
            Throw New ArgumentException
        End Function
    End Class

    Any code that calls the interface methods just has to provide an appropriate number of arguments for the particular instance.  It is up to you how you want to deal with parameter count or type mismatches within each class that implements the interface.  Ignoring excess arguments is easy enough but you have to decide how to handle missing arguments (use default values or throw errors?) and what to do with type mismatches (attempt an automatic conversion or throw errors?).  A lot depends on how you intend to use the interface methods (that is, why you wanted the common class model in the first place).



    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, March 7, 2018 6:23 PM
    Moderator