簡體   English   中英

Visual Basic-如何從測試用例中調用函數並為數組返回值

[英]Visual Basic - How to call a function from test case and return value for an array

是的-如果是“家庭調查”問題的版本,則為此。 我試圖了解一個特定的部分,而無法獲得整個問題的答案! 我有一個“ select case true”,可以將信息加載到我的數組中,其中一個數組用於確定一個家庭是否滿足貧困要求。 我正在使用一個子例程來運行第二個選擇案例,以確定該家庭是否滿足條件,以及是否確實將其添加到數組中。 此代碼有效,但它是意大利面條代碼-如何創建一個適用於所有縣的測試用例,而不是如下所示對每個縣使用一個用例?

'subroutine to load data to arrays, reset the form and call a new random ID number
Private Sub SubmitData()
    Dim intHouseHoldIncome As Integer = CInt(txtHouseholdIncome.Text)

    'as per the county and state , store values in array
    Select Case True
        Case cbxCountyState.Text = "Hamilton,OH"
            income(0) = income(0) + intHouseHoldIncome
            count(0) = count(0) + 1
            Call povertystatus_0()
        Case cbxCountyState.Text = "Butler,OH"
            income(1) = income(1) + intHouseHoldIncome
            count(1) = count(1) + 1
            Call povertystatus_1()
        Case cbxCountyState.Text = "Clermont,OH"
            income(2) = income(2) + intHouseHoldIncome
            count(2) = count(2) + 1
            Call povertystatus_2()
        Case cbxCountyState.Text = "Warren,OH"
            income(3) = income(3) + intHouseHoldIncome
            count(3) = count(3) + 1
            Call povertystatus_3()
        Case cbxCountyState.Text = "Campbell,KY"
            income(4) = income(4) + intHouseHoldIncome
            count(4) = count(4) + 1
            Call povertystatus_4()
        Case cbxCountyState.Text = "Boone,KY"
            income(5) = income(5) + intHouseHoldIncome
            count(5) = count(5) + 1
            Call povertystatus_5()
        Case cbxCountyState.Text = "Kenton,KY"
            income(6) = income(6) + intHouseHoldIncome
            count(6) = count(6) + 1
            Call povertystatus_6()
    End Select
    'Clear and re-focus inputs
    lblIdNumber.Text = ""
    cbxHouseholdSize.SelectedIndex = -1
    cbxCountyState.SelectedIndex = -1
    txtHouseholdIncome.Text = String.Empty
    'call random function to provide new id number
    Call IdNumber()
End Sub

'function to determine poverty status+++++++++++++++++++
Private Sub povertystatus_0()
    Dim intHouseHoldIncome As Integer = CInt(txtHouseholdIncome.Text)
    Dim intsize As Integer = CInt(cbxHouseholdSize.SelectedItem)
    Dim testincome As Integer
    Select Case intsize
        Case 1
            testincome = 10210
        Case 2
            testincome = 13690
        Case 3
            testincome = 17170
        Case 4
            testincome = 20650
        Case 5
            testincome = 24130
    End Select
    If intHouseHoldIncome <= testincome Then
        povertytrue(0) = povertytrue(0) + 1
    End If
End Sub

Private Sub povertystatus_1()
    Dim intHouseHoldIncome As Integer = CInt(txtHouseholdIncome.Text)
    Dim intsize As Integer = CInt(cbxHouseholdSize.SelectedItem)
    Dim testincome As Integer
    Select Case intsize
        Case 1
            testincome = 10210
        Case 2
            testincome = 13690
        Case 3
            testincome = 17170
        Case 4
            testincome = 20650
        Case 5
            testincome = 24130
    End Select
    If intHouseHoldIncome <= testincome Then
        povertytrue(1) = povertytrue(1) + 1
    End If
End Sub

Private Sub povertystatus_2()
    Dim intHouseHoldIncome As Integer = CInt(txtHouseholdIncome.Text)
    Dim intsize As Integer = CInt(cbxHouseholdSize.SelectedItem)
    Dim testincome As Integer
    Select Case intsize
        Case 1
            testincome = 10210
        Case 2
            testincome = 13690
        Case 3
            testincome = 17170
        Case 4
            testincome = 20650
        Case 5
            testincome = 24130
    End Select
    If intHouseHoldIncome <= testincome Then
        povertytrue(2) = povertytrue(2) + 1
    End If
End Sub

Private Sub povertystatus_3()
    Dim intHouseHoldIncome As Integer = CInt(txtHouseholdIncome.Text)
    Dim intsize As Integer = CInt(cbxHouseholdSize.SelectedItem)
    Dim testincome As Integer
    Select Case intsize
        Case 1
            testincome = 10210
        Case 2
            testincome = 13690
        Case 3
            testincome = 17170
        Case 4
            testincome = 20650
        Case 5
            testincome = 24130
    End Select
    If intHouseHoldIncome <= testincome Then
        povertytrue(3) = povertytrue(3) + 1
    End If
End Sub

Private Sub povertystatus_4()
    Dim intHouseHoldIncome As Integer = CInt(txtHouseholdIncome.Text)
    Dim intsize As Integer = CInt(cbxHouseholdSize.SelectedItem)
    Dim testincome As Integer
    Select Case intsize
        Case 1
            testincome = 10210
        Case 2
            testincome = 13690
        Case 3
            testincome = 17170
        Case 4
            testincome = 20650
        Case 5
            testincome = 24130
    End Select
    If intHouseHoldIncome <= testincome Then
        povertytrue(4) = povertytrue(4) + 1
    End If
End Sub

Private Sub povertystatus_5()
    Dim intHouseHoldIncome As Integer = CInt(txtHouseholdIncome.Text)
    Dim intsize As Integer = CInt(cbxHouseholdSize.SelectedItem)
    Dim testincome As Integer
    Select Case intsize
        Case 1
            testincome = 10210
        Case 2
            testincome = 13690
        Case 3
            testincome = 17170
        Case 4
            testincome = 20650
        Case 5
            testincome = 24130
    End Select
    If intHouseHoldIncome <= testincome Then
        povertytrue(5) = povertytrue(5) + 1
    End If
End Sub

Private Sub povertystatus_6()
    Dim intHouseHoldIncome As Integer = CInt(txtHouseholdIncome.Text)
    Dim intsize As Integer = CInt(cbxHouseholdSize.SelectedItem)
    Dim testincome As Integer
    Select Case intsize
        Case 1
            testincome = 10210
        Case 2
            testincome = 13690
        Case 3
            testincome = 17170
        Case 4
            testincome = 20650
        Case 5
            testincome = 24130
    End Select
    If intHouseHoldIncome <= testincome Then
        povertytrue(6) = povertytrue(6) + 1
    End If
End Sub

這不是意大利面條代碼。 它只包含很多重復。 您公然違反了DRY原則,即不要重復自己。

據我所知,除數組索引外,每種povertystatus方法都完全相同,因此,在這種情況下,通常要遵循的規則是,將該索引提取為參數並將所有這些方法折疊為一個:

Private Sub PovertyStatus(index As Integer)
    Dim intHouseHoldIncome As Integer = CInt(txtHouseholdIncome.Text)
    Dim intSize As Integer = CInt(cbxHouseholdSize.SelectedItem)
    Dim testIncome As Integer

    Select Case intSize
        Case 1
            testIncome = 10210
        Case 2
            testIncome = 13690
        Case 3
            testIncome = 17170
        Case 4
            testIncome = 20650
        Case 5
            testIncome = 24130
    End Select

    If intHouseHoldIncome <= testIncome Then
        povertyTrue(index) = povertyTrue(index) + 1
    End If
End Sub

請注意,我還更改了各種名稱的大小寫以便保持一致。

然后,您也可以修復您的第一個“ Select Case怪物。 這個:

Select Case True
    Case cbxCountyState.Text = "Hamilton,OH"
        income(0) = income(0) + intHouseHoldIncome
        count(0) = count(0) + 1
        Call povertystatus_0()
    Case cbxCountyState.Text = "Butler,OH"
        income(1) = income(1) + intHouseHoldIncome
        count(1) = count(1) + 1
        Call povertystatus_1()
    Case cbxCountyState.Text = "Clermont,OH"
        income(2) = income(2) + intHouseHoldIncome
        count(2) = count(2) + 1
        Call povertystatus_2()
    Case cbxCountyState.Text = "Warren,OH"
        income(3) = income(3) + intHouseHoldIncome
        count(3) = count(3) + 1
        Call povertystatus_3()
    Case cbxCountyState.Text = "Campbell,KY"
        income(4) = income(4) + intHouseHoldIncome
        count(4) = count(4) + 1
        Call povertystatus_4()
    Case cbxCountyState.Text = "Boone,KY"
        income(5) = income(5) + intHouseHoldIncome
        count(5) = count(5) + 1
        Call povertystatus_5()
    Case cbxCountyState.Text = "Kenton,KY"
        income(6) = income(6) + intHouseHoldIncome
        count(6) = count(6) + 1
        Call povertystatus_6()
End Select

變成這個:

Dim index = cbxCountyState.SelectedIndex

income(index) = income(index) + intHouseHoldIncome
count(index) = count(index) + 1
PovertyStatus()

您也確實不需要在PovertStatus使用Select Case 您可以將testIncome值放入數組中並對其進行索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM