0

I would like to create an Interface that is both typed and non typed, like IList and IList(Of T).

Example usage that I'm looking for. .


Dim a as IList(Of String)

test(a)

public sub test(value as IList){
   //I can use the Ilist here without worrying that its an IList(Of String)
} 

I would like to create an Interface called IRead that I can use like the IList Interfaces.

//I currently can not get this to work Msg(New test())

Any suggestions?


public Interface IRead(Of T)
    Get() As T
End

public Interface IRead()
   Get() as Object
End Interface

Public Class test
        Implements IRead(Of Integer)

        Public Function Get() As Integer Implements IRead(Of Integer).Get
            Return 35235
        End Function

End Class

private sub Msg(value as IRead)

  MsgBox(value.GetSingle)

End Sub

1 Answer 1

1

There are two ways to do this. First, you can define the two interfaces separately and then leave it up to each class to remember to implement both, like this:

Public Interface IRead
    Function GetValue() As Object
End Interface

Public Interface IRead(Of T)
    Function GetValue() As T
End Interface

Public Class MyReader
    Implements IRead(Of Integer)
    Implements IRead

    Public Function GetValue() As Integer Implements IRead(Of Integer).GetValue
        Return 35235
    End Function

    Private Function GetValueAsObject() As Object Implements IRead.GetValue
        Return GetValue()
    End Function
End Class

Or, if you want to force all classes which implement the generic interface to also implement the non-generic one, you can do it by having the generic interface inherit the other interface, like this:

Public Interface IRead
    Function GetValue() As Object
End Interface

Public Interface IRead(Of T)
    Inherits IRead
    Function GetValue() As T
End Interface

Public Class MyReader
    Implements IRead(Of Integer)

    Public Function GetValue() As Integer Implements IRead(Of Integer).GetValue
        Return 35235
    End Function

    Private Function GetValueAsObject() As Object Implements IRead.GetValue
        Return GetValue()
    End Function
End Class

The latter option is nice because the compiler won't let you forget to implement both interfaces, but it still doesn't save you from having to implement that extra pass-through method in every class. If you want to avoid having to write that extra method in every class, the only way to do that is with a base class, for instance:

Public MustInherit Class BaseReader(Of T)
    Implements IRead(Of T)

    Public MustOverride Function GetValue() As T Implements IRead(Of T).GetValue

    Private Function GetValueAsObject() As Object Implements IRead.GetValue
        Return GetValue()
    End Function
End Class

Public Class MyReader
    Inherits BaseReader(Of Integer)

    Public Overrides Function GetValue() As Integer
        Return 35235
    End Function
End Class
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.