How do you create a generic method in a class?

Posted by Seth Spearman on Stack Overflow See other posts from Stack Overflow or by Seth Spearman
Published on 2010-04-28T17:21:55Z Indexed on 2010/04/28 17:57 UTC
Read the original article Hit count: 161

Filed under:
|
|
|

Hello, I am really trying to follow the DRY principle. I have a sub that looks like this?

Private Sub DoSupplyModel

        OutputLine("ITEM SUMMARIES")
        Dim ItemSumms As New SupplyModel.ItemSummaries(_currentSupplyModel, _excelRows)
        ItemSumms.FillRows()
        OutputLine("")

        OutputLine("NUMBERED INVENTORIES")
        Dim numInvs As New SupplyModel.NumberedInventories(_currentSupplyModel, _excelRows)
        numInvs.FillRows()
        OutputLine("")   

End Sub

I would like to collapse these into a single method using generics. For the record, ItemSummaries and NumberedInventories are both derived from the same base class DataBuilderBase.

I can't figure out the syntax that will allow me to do ItemSumms.FillRows and numInvs.FillRows in the method.

FillRows is declared as Public Overridable Sub FillRows in the base class.

Thanks in advance.

EDIT
Here is my end result

Private Sub DoSupplyModels()

    DoSupplyModelType("ITEM SUMMARIES",New DataBlocks(_currentSupplyModel,_excelRows)
    DoSupplyModelType("DATA BLOCKS",New DataBlocks(_currentSupplyModel,_excelRows)

End Sub

Private Sub DoSupplyModelType(ByVal outputDescription As String, ByVal type As DataBuilderBase)
    OutputLine(outputDescription)
    type.FillRows()
    OutputLine("")
End Sub

But to answer my own question...I could have done this...

Private Sub DoSupplyModels()

    DoSupplyModelType(Of Projections)("ITEM SUMMARIES")
    DoSupplyModelType(Of DataBlocks)("DATA BLOCKS")

End Sub

Private Sub DoSupplyModelType(Of T as DataBuilderBase)(ByVal outputDescription As String, ByVal type As T)
    OutputLine(outputDescription)
    dim type as New T(_currentSupplyModel,_excelRows)
    type.FillRows()
    OutputLine("")
End Sub

Is that right? Does the New T() work?

Seth

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about c#