0

I wanted to refactor this big piece of bloated code ,I feel like there is a better way to rewrite this. Does anyone have idea of how to do it?

if (ls.SelectedIndex == 1) { grid1.Visibility = Visibility.Visible; }
...
if (ls.SelectedIndex == 58) { grid58.Visibility = Visibility.Visible; }
nir3434
  • 7
  • 3

1 Answers1

1

Create array of Grid

var gridArray = new Grid[N];

Fill it:

gridArray[1] = grid1;
gridArray[2] = grid2;
...

Use:

gridArray[ls.SelectedIndex].Visibility = Visibility.Visible;
Backs
  • 24,430
  • 5
  • 58
  • 85
  • You have to make sure to check for -1 selectedIndex or you will run into an error – npo Dec 12 '18 at 09:11