VB.Net如何根据计数器更改变量

时间:2014-09-17 20:10:40

标签: vb.net

我有一个For Next Loop,它将连接到数据库,然后搜索结果并将结果分配给变量。现在我以消息框的形式显示它。我希望用计数器改变该变量

我的节目:

For x = 1 to 5

            dtstartdate = dtpStartDate.Value
            dtenddate = dtpEndDate.Value.AddDays(1).AddSeconds(-1)


            Try
                connetionString = "Data Source=..."
                sql = "Select * discounts"
                connection = New SqlConnection(connetionString)
                connection.Open()
                command = New SqlCommand(sql, connection)
                adapter.SelectCommand = command
                adapter.SelectCommand.CommandTimeout = 130
                adapter.SelectCommand.Parameters.AddWithValue("@StartDate", dtstartdate)
                adapter.SelectCommand.Parameters.AddWithValue("@EndDate", dtenddate)
                adapter.Fill(ds)
                connection.Close()
                connection.Dispose()

            Catch ex As Exception
                MsgBox(ex.Message)

        For Each Result As DataRow In ds.Tables(0).Rows
            Select Case Result("Report")
                Case "TOTALS"
                    Select Case Result("Description")
                        Case "Coupons", "Coupons Tax-Free", "GC"
                            sum = sum + (Result("netAmt"))
                    End Select

            End Select
        Next

下一步

我如何分配" SUM"改变每个循环。例如:

如果x为1我喜欢" Sum"成为" Sum1":

For Each Result As DataRow In ds.Tables(0).Rows
                Select Case Result("Report")
                    Case "TOTALS"
                        Select Case Result("Description")
                            Case "Coupons", "Coupons Tax-Free", "GC"
                                sum1 = sum1 + (Result("netAmt"))
                        End Select

                End Select

如果x为2,我希望sum为sum2

For Each Result As DataRow In ds.Tables(0).Rows
                Select Case Result("Report")
                    Case "TOTALS"
                        Select Case Result("Description")
                            Case "Coupons", "Coupons Tax-Free", "GC"
                                sum2 = sum2 + (Result("netAmt"))
                        End Select

                End Select

如此依此类推。在节目结束时,我将显示" Sum1"," Sum2"," Sum3"在报告上。

2 个答案:

答案 0 :(得分:3)

为sum变量使用数组。你不必改变变量的名称(sum1,sum2,sum3),你只需要改变数组的索引(sum(1),sum(2),sum(3)......这看起来像sum( x))并将值存储在这些索引中。

答案 1 :(得分:0)

我要改变的第一件事是你的查询 如果我没有记错,您只对Report字段包含单词'TOTALS'Description字段包含单词'Coupons' or 'Coupons Tax-Free' or 'GC'的行感兴趣。

因此,您可以更改查询以表达WHERE条件 这将大大减少返回的行数和最终总计的复杂性 (顺便说一下,你确定你不需要增加@startDate参数吗?)

第二件事是使用List(Of Double)而不是单个变量

Dim sumTotals = new List(Of Double)()
For x = 1 to 5
   ....
   sql = "Select * discounts WHERE Report = 'TOTALS' " & _ 
                             "AND Description IN('Coupons', 'Coupons Tax-Free, 'GC'"
   ....

   Dim partialSum As Double
   For Each Result In ds.Tables(0).Rows
       partialSum = partialSum + (Result("netAmt"))
   Next
   sumTotals.Add(partialSum)
Next

现在,在For循环结束时,你有一个5个双值的列表(每天一个),你可以用作普通数组

For each x in sumTotals
   Console.WriteLine(x)
Next
....

或应用Linq扩展

Dim grandTotal = sumTotals.Sum()