C
C#9mo ago
Wacku

Is there a better way to do this? (multiple switch statements with repetitive names)

Hey there, I'm new to C#, so forgive me if this question seems a bit stupid. To Explain, these are lists ObservableCollection lists. if a list has no items, it is not added to the master list. each switch case validates whether or not to add the list. In the actual application there are 20 lists, but I have included 3 of the statements because they are all the same. If I am not being clear enough, please let me know, Thank you.
switch (group1.Items.Count)
{
case 0:
break;
default:
this.AllGroups.Add(group1);
break;
}
switch (group2.Items.Count)
{
case 0:
break;
default:
this.AllGroups.Add(group2);
break;
}
switch (group3.Items.Count)
{
case 0:
break;
default:
this.AllGroups.Add(group3);
break;
}
switch (group1.Items.Count)
{
case 0:
break;
default:
this.AllGroups.Add(group1);
break;
}
switch (group2.Items.Count)
{
case 0:
break;
default:
this.AllGroups.Add(group2);
break;
}
switch (group3.Items.Count)
{
case 0:
break;
default:
this.AllGroups.Add(group3);
break;
}
3 Replies
Wacku
WackuOP9mo ago
Discord might be screwing up the formatting a bit, here's a screenshot
No description
Jimmacle
Jimmacle9mo ago
you can start by just using if statements you're only checking one condition
if (groupX.Items.Count != 0)
AllGroups.Add(groupX);
if (groupX.Items.Count != 0)
AllGroups.Add(groupX);
Wacku
WackuOP9mo ago
Oh, how silly of me, I don't know how I didn't think of that Thank you very much
Want results from more Discord servers?
Add your server