C
C#2y ago
Matt

❔ How to shorten switch statement

public void SwitchAvailability(string koalaName)
{
switch(koalaName)
{
case "Boonie":
BoonieAvailable = !BoonieAvailable;
break;
case "Mim":
MimAvailable = !MimAvailable;
break;
case "Snugs":
SnugsAvailable = !SnugsAvailable;
break;
case "Gummy":
GummyAvailable = !GummyAvailable;
break;
case "Dubbo":
DubboAvailable = !DubboAvailable;
break;
case "Elizabeth":
ElizabethAvailable = !ElizabethAvailable;
break;
case "Kiki":
KikiAvailable = !KikiAvailable;
break;
case "Katie":
KatieAvailable = !KatieAvailable;
break;
};
}
public void SwitchAvailability(string koalaName)
{
switch(koalaName)
{
case "Boonie":
BoonieAvailable = !BoonieAvailable;
break;
case "Mim":
MimAvailable = !MimAvailable;
break;
case "Snugs":
SnugsAvailable = !SnugsAvailable;
break;
case "Gummy":
GummyAvailable = !GummyAvailable;
break;
case "Dubbo":
DubboAvailable = !DubboAvailable;
break;
case "Elizabeth":
ElizabethAvailable = !ElizabethAvailable;
break;
case "Kiki":
KikiAvailable = !KikiAvailable;
break;
case "Katie":
KatieAvailable = !KatieAvailable;
break;
};
}
I feel like a noob again. I can't think of how to remove the switch statement. I think I need either the ref keyword or reflections but not sure which and not sure what to do
8 Replies
canton7
canton72y ago
Looks like you can't return ref from a switch, annoyingly Do you need however-many separate properties? Do you actually use them as properties?
Matt
MattOP2y ago
yes they are required properties since they're bound to UI controls for WPF
canton7
canton72y ago
You can bind to e.g. dictionary elements in WPF, if that makes life easier <TextBlock Text="{Binding Koalas["Kiki"]}"/>
Matt
MattOP2y ago
so I could bind to a dictionary of type string bool and get the bool by string?
canton7
canton72y ago
It's one option
Matt
MattOP2y ago
that might make my life significantly easier actually. I'm not the one working on the front end but I'll put the suggestion forwards
canton7
canton72y ago
That or something like:
class Koala
{
public string Name { get; }
public bool IsAvailable { get; set; }

public Koala(string name) => Name = name;
}

class KoalaCollection
{
public Koala Boonie { get; }
public Koala Mim { get; }

private readonly Dictionary<string, Koala> koalas = new();

public KoalaCollection()
{
Koala Add(string name)
{
var koala = new Koala(name);
this.koalas[name] = koala;
return koala;
}

Boonie = Add("Boonie");
Mim = Add("Mim");
}

public Koala GetByName(string name) => koalas[name];
}
class Koala
{
public string Name { get; }
public bool IsAvailable { get; set; }

public Koala(string name) => Name = name;
}

class KoalaCollection
{
public Koala Boonie { get; }
public Koala Mim { get; }

private readonly Dictionary<string, Koala> koalas = new();

public KoalaCollection()
{
Koala Add(string name)
{
var koala = new Koala(name);
this.koalas[name] = koala;
return koala;
}

Boonie = Add("Boonie");
Mim = Add("Mim");
}

public Koala GetByName(string name) => koalas[name];
}
Then "{Binding Koalas.Mim.IsAvailable}" Then you can do:
public void SwitchAvailability(string koalaName)
{
var koala = koalas.GetByName(koalaName);
koala.IsAvailable = !koala.IsAvailable;
}
public void SwitchAvailability(string koalaName)
{
var koala = koalas.GetByName(koalaName);
koala.IsAvailable = !koala.IsAvailable;
}
(Add INPC as appropriate)
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server