[英]Visual Basic - How to call a function from test case and return value for an array
Yes - this if for a version of the "household survey" problem. 是的-如果是“家庭调查”问题的版本,则为此。 I'm trying to understand one specific part not get the answer for the whole problem! 我试图了解一个特定的部分,而无法获得整个问题的答案! I have a "select case true" to load information into my arrays, and one of the arrays is used to determine if a household meets poverty requirements or not. 我有一个“ select case true”,可以将信息加载到我的数组中,其中一个数组用于确定一个家庭是否满足贫困要求。 I am using a subroutine to run a second select case to determine if the household meets the criteria and if it does add that to an array. 我正在使用一个子例程来运行第二个选择案例,以确定该家庭是否满足条件,以及是否确实将其添加到数组中。 This code works but it is spaghetti code - how can I create one test case that works for all the counties instead of using one for each as below? 此代码有效,但它是意大利面条代码-如何创建一个适用于所有县的测试用例,而不是如下所示对每个县使用一个用例?
'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
It's not spaghetti code. 这不是意大利面条代码。 It just contains A LOT of repetition. 它只包含很多重复。 You are flagrantly violating the DRY principle, ie don't repeat yourself. 您公然违反了DRY原则,即不要重复自己。
As far as I can tell, each of your povertystatus
methods is exactly the same except for the array index so, as is always the rule in such cases, extract that index out as a parameter and collapse all those methods into one: 据我所知,除数组索引外,每种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
Note that I've also changed the casing of various names there to be consistent. 请注意,我还更改了各种名称的大小写以便保持一致。
You can then fix your first Select Case
monstrosity too. 然后,您也可以修复您的第一个“ Select Case
怪物。 This: 这个:
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
becomes this: 变成这个:
Dim index = cbxCountyState.SelectedIndex
income(index) = income(index) + intHouseHoldIncome
count(index) = count(index) + 1
PovertyStatus()
You really don't need a Select Case
in PovertStatus
either. 您也确实不需要在PovertStatus
使用Select Case
。 You can just put your testIncome
values into an array and index it too. 您可以将testIncome
值放入数组中并对其进行索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.