2

私は現在巨大なIFステートメントを持っており、それを可能な限り最小化したいと考えています。

SQLクエリから入力しているデータグリッドがあり、このデータグリッドから値を個別のラベルに渡します。

カウンターが7に達するまで変数を循環するForEachループを作成できます。ただし、ラベル名の値を1つ増やす必要がある場合に問題が発生します。毎回、基本的にラベル名にカウンター変数を追加する必要があります。

最小化する必要があるコードは次のとおりです。

result73 = DataGridView1.Rows(0).Cells(0).Value.ToString
result74 = DataGridView1.Rows(0).Cells(1).Value.ToString
result75 = DataGridView1.Rows(1).Cells(0).Value.ToString
result76 = DataGridView1.Rows(1).Cells(1).Value.ToString
result77 = DataGridView1.Rows(2).Cells(0).Value.ToString
result78 = DataGridView1.Rows(2).Cells(1).Value.ToString
result79 = DataGridView1.Rows(3).Cells(0).Value.ToString
result80 = DataGridView1.Rows(3).Cells(1).Value.ToString
result81 = DataGridView1.Rows(4).Cells(0).Value.ToString
result82 = DataGridView1.Rows(4).Cells(1).Value.ToString
result83 = DataGridView1.Rows(5).Cells(0).Value.ToString
result84 = DataGridView1.Rows(5).Cells(1).Value.ToString
result85 = DataGridView1.Rows(6).Cells(0).Value.ToString
result86 = DataGridView1.Rows(6).Cells(1).Value.ToString
result87 = DataGridView1.Rows(7).Cells(0).Value.ToString
result88 = DataGridView1.Rows(7).Cells(1).Value.ToString

If result73 = "Monday" Then
    DaySalesLbl1.Text = result74
ElseIf result73 = "Tuesday" Then
    DaySalesLbl2.Text = result74
ElseIf result73 = "Wednesday" Then
    DaySalesLbl3.Text = result74
ElseIf result73 = "Thursday" Then
    DaySalesLbl4.Text = result74
ElseIf result73 = "Friday" Then
    DaySalesLbl5.Text = result74
ElseIf result73 = "Saturday" Then
    DaySalesLbl6.Text = result74
ElseIf result73 = "Sunday" Then
    DaySalesLbl7.Text = result74
End If

このIfステートメントは、上記で宣言された変数ごとに実行されます。

私が作成したループは次のようになります。

Dim cou As Integer
Dim n As Integer
cou = 0
n = 1
Do Until result74 <> ""
    If result73 = cou Then
        DaySalesLbl +n = result74
    End If
    cou = cou + 1
    n = n + 1
Loop

ただし、DaySalesLbl +n = result74生成されたメソッドを探すため、このセクションではエラーが発生します。

4

2 に答える 2

0

次のようにコードを記述します。

For i as Integer = 0 to 7
   Select Case DataGridView1.Rows(i).Cells(1).Value.ToString 
    Case "Monday"
       Me.Controls("DaySalesLbl" & i+1).Text = DataGridView1.Rows(i).Cells(2).Value.ToString 
    Case "Tuesday"
       Me.Controls("DaySalesLbl" & i+2).Text = DataGridView1.Rows(i).Cells(2).Value.ToString 

   ... etc ...

私はこれを IDE なしで行ったので、何も台無しにしていないことを願っていますが、これがあなたを正しい方向に導き、助けてくれることを願っています。

于 2013-01-02T17:42:31.437 に答える