Mark S Fernando
Mark S Fernando

Reputation: 3

How to minimized repet coding in Select Case

I Want to know how to write common coding for Select Case.

Select Case cboReportType.SelectedValue.ToString.Substring(0, 1)
            Case Is = "02"
                Dim crReport As New crReportPage
                With crReport
                    .SetDatabaseLogon(strUser, strPass)
                    .SetParameterValue("@Parameter", cboReportType.SelectedValue.ToString.Substring(0, 2))
                    .SetParameterValue("@SortingOrder", cboSortingOrder.SelectedValue.ToString.Substring(0, 1))
                End With
            Case Is = "03"
                Dim crReport As New crReportRateCategory
                With crReport
                    .SetDatabaseLogon(strUser, strPass)
                    .SetParameterValue("@Parameter", cboReportType.SelectedValue.ToString.Substring(0, 2))
                    .SetParameterValue("@SortingOrder", cboSortingOrder.SelectedValue.ToString.Substring(0, 1))
                End With
            Case Is = "04"
                Dim crReport As New crReportRateSubCategory
                With crReport
                    .SetDatabaseLogon(strUser, strPass)
                    .SetParameterValue("@Parameter", cboReportType.SelectedValue.ToString.Substring(0, 2))
                    .SetParameterValue("@SortingOrder", cboSortingOrder.SelectedValue.ToString.Substring(0, 1))
                End With
            Case Is = "05", "06", "07"
                Dim crReport As New crReportMaterialColourBox
                With crReport
                    .SetDatabaseLogon(strUser, strPass)
                    .SetParameterValue("@Parameter", cboReportType.SelectedValue.ToString.Substring(0, 2))
                    .SetParameterValue("@SortingOrder", cboSortingOrder.SelectedValue.ToString.Substring(0, 1))
                End With
            Case Is = "08"
                Dim crReport As New crReportPosition
                With crReport
                    .SetDatabaseLogon(strUser, strPass)
                    .SetParameterValue("@Parameter", cboReportType.SelectedValue.ToString.Substring(0, 2))
                    .SetParameterValue("@SortingOrder", cboSortingOrder.SelectedValue.ToString.Substring(0, 1))
                End With
        End Select

In above cording there are common (repeating) cording. I want to know how to put all in one section.

Upvotes: 0

Views: 21

Answers (1)

Caius Jard
Caius Jard

Reputation: 74615

For all these things to even have the same properties/methods they must surely descend from a common parent object, or implement a common interface (and if they don't, they should) meaning you can:

    Dim crReport As ParentTypeOrInterface
    
    Select Case cboReportType.SelectedValue.ToString.Substring(0, 1)
        Case "02"
            crReport = New crReportPage
            
        Case "03"
            crReport = New crReportRateCategory
            
        Case "04"
            ...
     End Select

     With crReport
         .SetDatabaseLogon(strUser, strPass)
         .SetParameterValue("@Parameter", cboReportType.SelectedValue.ToString.Substring(0, 2))
         .SetParameterValue("@SortingOrder", cboSortingOrder.SelectedValue.ToString.Substring(0, 1))
     End With

Upvotes: 1

Related Questions