2017-04-23 62 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

非常感谢!我对阵列没有很好的把握 - 我可以让我的代码工作,但总是“战争与和平”的长度。 –