2
\$\begingroup\$

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
 Else
 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
asked Aug 8, 2013 at 22:03
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

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
 Next
 For Each ctrl in grpHide
 ctrl.Visible = Not show
 Next
End Sub
answered Aug 12, 2013 at 12:06
\$\endgroup\$
2
  • \$\begingroup\$ This does cut things down 1/2 at least, ty. \$\endgroup\$ Commented Aug 12, 2013 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\$ Commented Aug 13, 2013 at 7:05
1
\$\begingroup\$

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
 Else
 col.Visible = Not Show
 End If
Next
answered Oct 17, 2013 at 21:06
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.