Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Dynamically name Variable in loop

$
0
0

I wrote was able to shorten some repeative code using a for loop and the findcontrol.

I have a piece of code I am struggling with adapting to this stragedy; I need the name of a variable to change with the value of i during the loop

ie: the value of i is 1 the variable name is rows1  i is 2  rows2 etc...

before code:

Dim gvd1 As New DataDisplay
        gvd1.Display = "p_DisplayBills2"
        gvd1.DisplayD = String.Format("p_DisplayBills2 {0}, 1", month)
        GridView1.DataSource = gvd1.DataDisplay()
        GridView1.DataBind()

        Dim gvd2 As New DataDisplay
        gvd2.Display = "p_DisplayBills2"
        gvd2.DisplayD = String.Format("p_DisplayBills2 {0}, 2", month)
        GridView2.DataSource = gvd2.DataDisplay()
        GridView2.DataBind()

        Dim gvd3 As New DataDisplay
        gvd3.Display = "p_DisplayBills2"
        gvd3.DisplayD = String.Format("p_DisplayBills2 {0}, 3", month)
        GridView3.DataSource = gvd3.DataDisplay()
        GridView3.DataBind()

        Dim gvd4 As New DataDisplay
        gvd4.Display = "p_CalulatePayPerBills"
        gvd4.DisplayD = String.Format("p_CalulatePayPerBills {0}, 1", month)
        GridView4.DataSource = gvd4.DataDisplay()
        GridView4.DataBind()

        Dim gvd5 As New DataDisplay
        gvd5.Display = "p_CalulatePayPerBills"
        gvd5.DisplayD = String.Format("p_CalulatePayPerBills {0}, 2", month)
        GridView5.DataSource = gvd5.DataDisplay()
        GridView5.DataBind()

        Dim gvd6 As New DataDisplay
        gvd6.Display = "p_CalulatePayPerBills"
        gvd6.DisplayD = String.Format("p_CalulatePayPerBills {0}, 3", month)
        GridView6.DataSource = gvd6.DataDisplay()
        GridView6.DataBind()

        Dim rows1 As Integer = GridView1.Rows.Count()
        Dim rows2 As Integer = GridView2.Rows.Count()
        Dim rows3 As Integer = GridView3.Rows.Count()
        Dim arow As Integer() = {rows1, rows2, rows3}
        Dim larrow As Integer = arow.Max()

After code ( the problem)

     Dim gvs As GridView

        For i As Integer = 1 To 6
            Dim gvd As New DataDisplay
            If i < 4 Then
                gvd.Name = "gvd" & i
                gvd.Display = "p_DisplayBills2"
                gvd.DisplayD = String.Format("p_DisplayBills2 {0}, {1}", month, i)

            End If
            If i > 3 And i < 7 Then
                Dim cal As Integer = i - 3
                gvd.Name = "gvd" & i
                gvd.Display = "p_CalulatePayPerBills"
                gvd.DisplayD = String.Format("p_CalulatePayPerBills {0}, {1}", month, cal)
            End If
            gvs = FindControl("GridView" & i)
            gvs.DataSource = gvd.DataDisplay()
            gvs.DataBind()

            'Dim rows As New rows
            If i < 4 Then
                'how to make the name of the variable "rows" & i     
                Dim rowsi = gvs.Rows.Count()
                ' then determine the max of all the rowsi
                Dim rowmax As Integer = rowsi.max()
                'so it does the same thing as the code below
                'Dim rows1 As Integer = GridView1.Rows.Count()
                'Dim rows2 As Integer = GridView2.Rows.Count()
                'Dim rows3 As Integer = GridView3.Rows.Count()
                'Dim arow As Integer() = {rows1, rows2, rows3}
                'Dim larrow As Integer = arow.Max()
            End If


        Next

I also thought about using the following approach but the rows variables are not available outside of the While statements

        Dim gvs As GridView

        For i As Integer = 1 To 6
            Dim gvd As New DataDisplay
            If i < 4 Then
                gvd.Name = "gvd" & i
                gvd.Display = "p_DisplayBills2"
                gvd.DisplayD = String.Format("p_DisplayBills2 {0}, {1}", month, i)

            End If
            If i > 3 And i < 7 Then
                Dim cal As Integer = i - 3
                gvd.Name = "gvd" & i
                gvd.Display = "p_CalulatePayPerBills"
                gvd.DisplayD = String.Format("p_CalulatePayPerBills {0}, {1}", month, cal)
            End If
            gvs = FindControl("GridView" & i)
            gvs.DataSource = gvd.DataDisplay()
            gvs.DataBind()


            If i < 4 Then
                While i = 1
                    Dim rows1 As Integer = gvs.Rows.Count()
                End While
                While i = 2
                    Dim rows2 As Integer = gvs.Rows.Count()
                End While
                While i = 3
                    Dim rows3 As Integer = gvs.Rows.Count()
                End While
                Dim arow As Integer() = {rows1, rows2, rows3}
                Dim larrow As Integer = arow.Max()

            End If


        Next




Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>