2017-04-23 64 views
0

是 - 如果是「住戶調查」問題的版本,則爲此。我試圖理解一個特定的部分沒有得到整個問題的答案!我有一個「select case true」將信息加載到我的數組中,其中一個數組用於確定家庭是否符合貧困條件。我正在使用一個子例程來運行第二個選擇案例來確定家庭是否符合條件,以及它是否將其添加到數組中。這段代碼有效,但它是意大利麪代碼 - 我如何創建一個適用於所有縣的測試用例,而不是像下面那樣爲每個用戶都使用一個?Visual Basic - 如何從測試用例調用函數併爲數組返回值

'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 
+0

您必須開始考慮如何將此硬編碼數據放入文件或dbase中。所以它可以很容易地改變和擴展。一旦你有了,你沒有選擇,只能正確地寫。 –

+0

使用參數。 – N0Alias

回答

1

這不是意大利麪代碼。它只是包含很多重複。你公然違反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值放入一個數組中並將其編入索引。

+0

非常感謝!我對陣列沒有很好的把握 - 我可以讓我的代碼工作,但總是「戰爭與和平」的長度。 –

相關問題