I have this code snippet below that I am looking for a way to make smaller if possible. Any help or suggestions would be appreciated, thanks!

Private Sub ShowAlternateRows(Show As Boolean)
    If Show = True Then
        splitter.Visible = True
        lblAlternateActualPercent.Visible = True
        lblAltEstimated.Visible = True
        lblStaticAltEstimated.Visible = True

        Grid.Cols.Item("Alternate Type").Visible = True
        Grid.Cols.Item("Alternate Start").Visible = True
        Grid.Cols.Item("Alternate End").Visible = True
        Grid.Cols.Item("Alternate Hours").Visible = True
        Grid.Cols.Item("Alternate Burn").Visible = True

        Grid.Cols.Item("Start Estimated").Visible = False
        Grid.Cols.Item("End Estimated").Visible = False
        Grid.Cols.Item("Start Actual").Visible = False
        Grid.Cols.Item("End Actual").Visible = False
        Grid.Cols.Item("Estimated Hours").Visible = False
        Grid.Cols.Item("Actual Hours").Visible = False
        Grid.Cols.Item("Burn Estimated").Visible = False
        Grid.Cols.Item("Object Count").Visible = False
        Grid.Cols.Item("Request Count").Visible = False
        Grid.Cols.Item("Resource Count").Visible = False

        lblStaticActual.Visible = False
        lblActual.Visible = False
        lblActualPercent.Visible = False
        lblStaticEstimated.Visible = False
        lblEstimated.Visible = False
        lblStaticFiltered.Visible = False
        lblFiltered.Visible = False
        lblFilteredPercent.Visible = False

        splitter.Visible = False
        lblAlternateActualPercent.Visible = False
        lblAltEstimated.Visible = False
        lblStaticAltEstimated.Visible = False

        lblStaticActual.Visible = True
        lblActual.Visible = True
        lblActualPercent.Visible = True
        lblStaticEstimated.Visible = True
        lblEstimated.Visible = True
        lblStaticFiltered.Visible = True
        lblFiltered.Visible = True
        lblFilteredPercent.Visible = True

        Grid.Cols.Item("Start Estimated").Visible = True
        Grid.Cols.Item("End Estimated").Visible = True
        Grid.Cols.Item("Start Actual").Visible = True
        Grid.Cols.Item("End Actual").Visible = True
        Grid.Cols.Item("Estimated Hours").Visible = True
        Grid.Cols.Item("Actual Hours").Visible = True
        Grid.Cols.Item("Burn Estimated").Visible = True
        Grid.Cols.Item("Object Count").Visible = True
        Grid.Cols.Item("Request Count").Visible = True
        Grid.Cols.Item("Resource Count").Visible = True

        Grid.Cols.Item("Alternate Type").Visible = False
        Grid.Cols.Item("Alternate Start").Visible = False
        Grid.Cols.Item("Alternate End").Visible = False
        Grid.Cols.Item("Alternate Hours").Visible = False
        Grid.Cols.Item("Alternate Burn").Visible = False
    End If
End Sub

Assuming OPTION STRICT OFF (since I don't know the type of Grid.Cols.Item):

Private Sub ShowAlternateRows(show As Boolean)
    Dim grpShow = 
        splitter, lblAlternateActualPercent, 
        lblAltEstimated, lblStaticAltEstimated,  
        Grid.Cols.Item("Alternate Type"), Grid.Cols.Item("Alternate Start"),
        Grid.Cols.Item("Alternate End"),  Grid.Cols.Item("Alternate Hours"),
        Grid.Cols.Item("Alternate Burn")
    Dim grpHide = 
        Grid.Cols.Item("Start Estimated"), Grid.Cols.Item("End Estimated"),
        Grid.Cols.Item("Start Actual"),    Grid.Cols.Item("End Actual"),
        Grid.Cols.Item("Estimated Hours"), Grid.Cols.Item("Actual Hours"),
        Grid.Cols.Item("Burn Estimated"),  Grid.Cols.Item("Object Count"),
        Grid.Cols.Item("Request Count"),   Grid.Cols.Item("Resource Count"),
        lblStaticActual,  lblActual,
        lblActualPercent, lblStaticEstimated,
        lblEstimated, lblStaticFiltered,
        lblFiltered,  lblFilteredPercent
    For Each ctrl in grpShow
        ctrl.Visible = show
    For Each ctrl in grpHide
        ctrl.Visible = Not show
End Sub
  • \$\begingroup\$ This does cut things down 1/2 at least, ty. \$\endgroup\$ – Adam Aug 12 '13 at 21:55
  • \$\begingroup\$ @AdamShip Yeah; also, the code duplication is removed. You could save 4 more lines by replacing the For Each loops by using lists and using the ForEach() extension method, but I would not recommend doing so because of the loss of readability. \$\endgroup\$ – sloth Aug 13 '13 at 7:05

First, if you can put the labels in 1 or more panels, you can just show/hide the panels. This means instead of:

splitter.Visible = Show
lblAlternateActualPercent.Visible = Show
lblAltEstimated.Visible = Show
lblStaticAltEstimated.Visible = Show

lblStaticActual.Visible = Not Show
lblActual.Visible = Not Show
lblActualPercent.Visible = Not Show
lblStaticEstimated.Visible = Not Show
lblEstimated.Visible = Not Show
lblStaticFiltered.Visible = Not Show
lblFiltered.Visible = Not Show
lblFilteredPercent.Visible = Not Show

You could do something like:

Panel1.Visible = Show
Panel2.Visible. Not Show

Also, noticed how I used Show or Not Show instead if an If/Else statement with all the code twice.

Last, Itererate through the columns, assuming that you want to do something with each column. (I am assuming Grid.Cols returns a DataGridColumnCollection. You might need to change for your specific type)

For each col as DataGridColumn in Grid.Cols
  If col.Name.tolower.StartsWith("alternate ") then
    col.visible = Show
    col.Visible = Not Show
  End If

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.