我有7个不同的区域,并设定了不同的数量目标。我想输入if else语句,但是现在找不到将我的语句合并为一个方法。我正在重复我的功能。如何将这两个if else语句合并为一个?如果没有,我的编码将不断重复并变得很长。
Dim quantity As Integer = 0
Dim count As Integer = 0
For y = 0 To dt.Rows.Count - 1
quantity += CDbl(dt.Rows(y).Item(3))
Next
If Empty = False Then
Session("outputData") = Nothing
gvOutput.DataSource = Nothing
gvOutput.DataBind()
outputSave.Visible = False
Else
For y = 0 To dt.Rows.Count - 1
If dt.Rows(y).Item(7).ToString = "Malaysia" Then
If quantity >= 50 Or quantity < 50 And dt.Rows(y).Item(8).ToString <> "" Then
count += 1
dt.Columns(6).ColumnName = "Date"
Session("outputData") = TryCast(dt, DataTable)
Session("outputData4Search") = Nothing
messErrorOutput.Text = ""
'Bind Data to GridView
gvOutput.Caption = Path.GetFileName(FilePath)
Try
messErrorOutput.Text = ""
gvOutput.DataSource = dt
gvOutput.DataBind()
gvDisplayOutput.DataSource = CType(Session("outputData4Search"), DataTable)
gvDisplayOutput.DataBind()
outputSave.Visible = True
gvDisplayOutput.Visible = False
lblOutputCount.Text = ""
Catch ex As Exception
messErrorOutput.Text = "Please upload correct format of excel template file."
outputSave.Visible = False
gvOutput.DataSource = Nothing
gvOutput.DataBind()
End Try
ElseIf quantity < 50 And count = 0 Then
messErrorOutput.Text = "Please insert remarks in template file."
Session("outputData") = Nothing
gvOutput.DataSource = Nothing
gvOutput.DataBind()
outputSave.Visible = False
End If
End If
If dt.Rows(y).Item(7).ToString = "Singapore" Then
If quantity >= 90 Or quantity < 90 And dt.Rows(y).Item(8).ToString <> "" Then
count += 1
dt.Columns(6).ColumnName = "Date"
Session("outputData") = TryCast(dt, DataTable)
Session("outputData4Search") = Nothing
messErrorOutput.Text = ""
'Bind Data to GridView
gvOutput.Caption = Path.GetFileName(FilePath)
Try
messErrorOutput.Text = ""
gvOutput.DataSource = dt
gvOutput.DataBind()
gvDisplayOutput.DataSource = CType(Session("outputData4Search"), DataTable)
gvDisplayOutput.DataBind()
outputSave.Visible = True
gvDisplayOutput.Visible = False
lblOutputCount.Text = ""
Catch ex As Exception
messErrorOutput.Text = "Please upload correct format of excel template file."
outputSave.Visible = False
gvOutput.DataSource = Nothing
gvOutput.DataBind()
End Try
ElseIf quantity < 90 And count = 0 Then
messErrorOutput.Text = "Please insert remarks in template file."
Session("outputData") = Nothing
gvOutput.DataSource = Nothing
gvOutput.DataBind()
outputSave.Visible = False
End If
End If
Next
End If
答案 0 :(得分:1)
@Gadzin提到了处理重复块的功能。您可以使用If
Select
问题
Dim totalCount as Long = 0
For y = 0 To dt.Rows.Count - 1
Select Case dt.Rows(y).Item(7).ToString
Case "Malaysia"
totalCount =+ DataSomething1(50, y, dt.Rows(y).Item(8).ToString)
Case "Singapore"
totalCount =+ DataSomething1(90, y, dt.Rows(y).Item(8).ToString)
Case Else
'[...]
End Select
Next y
使用带有如下签名的修改函数:
Private Function DataSomething1(TestingValue As Integer, rowNum as Long, otherDataPoint) As Long
Dim quantity As Integer = 0
Dim count As Integer = 0
If quantity >= TestingValue Or quantity < TestingValue And otherDataPoint <> "" Then
count += 1
'[.... Some relevant code here]
ElseIf quantity < TestingValue And count = 0 Then
'[.... Some relevant code here]
End If
End If
Return count
End Function
答案 1 :(得分:0)
如果程序中出现重复的“代码块”,则应使用function
Private Function DataSomething(country As String, TestingValue As Integer)
Dim quantity As Integer = 0
Dim count As Integer = 0
If dt.Rows(y).Item(7).ToString = country Then
If quantity >= TestingValue Or quantity < TestingValue And dt.Rows(y).Item(8).ToString <> "" Then
count += 1
dt.Columns(6).ColumnName = "Date"
Session("outputData") = TryCast(dt, DataTable)
Session("outputData4Search") = Nothing
messErrorOutput.Text = ""
'Bind Data to GridView
gvOutput.Caption = Path.GetFileName(FilePath)
Try
messErrorOutput.Text = ""
gvOutput.DataSource = dt
gvOutput.DataBind()
gvDisplayOutput.DataSource = CType(Session("outputData4Search"), DataTable)
gvDisplayOutput.DataBind()
outputSave.Visible = True
gvDisplayOutput.Visible = False
lblOutputCount.Text = ""
Catch ex As Exception
messErrorOutput.Text = "Please upload correct format of excel template file."
outputSave.Visible = False
gvOutput.DataSource = Nothing
gvOutput.DataBind()
End Try
ElseIf quantity < TestingValue And count = 0 Then
messErrorOutput.Text = "Please insert remarks in template file."
Session("outputData") = Nothing
gvOutput.DataSource = Nothing
gvOutput.DataBind()
outputSave.Visible = False
End If
End If
Return somestuffifyouneed
End Function
Private Sub YourSub(sender As Object, e As EventArgs) Handles something
DataSomething(singapore, 50)
DataSomething(Malaysia, 90)
End Sub