How to create a universal method in a class?

I'm really trying to follow the DRY principle. Do 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 roll them into one method using generics. For recording, ItemSummaries and NumberedInventories are produced from the same base class DataBuilderBase.

I can not understand the syntax that will allow me to do ItemSumms.FillRows and numInvs .FillRows in the method.

FillRows is declared as Public Overridable Sub FillRowsin 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 question ... I could do it ...

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 DataBuilderBase (_currentSupplyModel,_excelRows)
    type.FillRows()
    OutputLine("")
End Sub

Is it correct?

Set

+3
source share
4 answers

, , , , :

Private Sub MyMethod(Of T As DataBuilderBase)(ByVal instance As T)
    instance.FillRows()
End Sub

, :

MyMethod(Of ItemSummaries)(new SupplyModel.ItemSummaries(...))
+3

, , : (VB , )

:

Private Sub FillAndOutput(textToOutput as String, filler as DataBuilderBase)
    OutputLine(string)        
    filler.FillRows()
    OutputLine("")
end sub

:

Private Sub DoSupplyModel
    FillAndOutput("ITEM SUMMARIES",New SupplyModel.ItemSummaries(_currentSupplyModel, _excelRows))
    FillAndOutput("NUMBERED INVENTORIES",New SupplyModel.NumberedInventories(_currentSupplyModel, _excelRows))        

End Sub
+1

, T , FillRows. #

private void myFunction<T>( T someList ) where T : DataBuilderBase {
    someList.FillRows();
}

VB.NET MSDN.

EDIT, , , , .

+1

, , . :

  • , , , .
  • , .

( ).

, , VB.NET of

Public Sub Foo(Of T)(argument as T)
   ...
End Sub
0

Source: https://habr.com/ru/post/1743223/


All Articles