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
Matt2y 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
Matt2y 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
Matt2y 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
More Posts
❔ BinaryFormatter does not serialize/deserialize the Tags of listview itemsso i have a listview that i want to serialize and then desirialize. I store some values in the tags ❔ ✅ Using generics that implement IEqualityOperators results in ambiguous call to the == operatorIf I have some `T: IEqualityOperators<T, T, bool>` and use a LINQ query such as `myTs.FirstOrDefault✅ Simple questionI have a code which ```cs if ("any" is "item1" or "item2" or "any") { Console.WriteLine("okay");Hi im not sure that my code is working as it should(class in c sharp)providing code here👇(When i add +1 or +10 to the GetX or GetY isn't it supossed to change?)❔ VISUAL STUDIO C# DATABASE AND LISTBOXI am currently working on my assignment and am stuck on this issue Where I have a Database to fetch❔ Sprites are invisible in build?i can see everything in the engine (unity) and ive gone over a bunch of things even checking my came❔ Hi,my flappy bird game script isnt workingThe script in making is to show that the score counter incrreases when the gamobject(bird) collides Hi i need some help with classes, im pretty new to this and i have an error with my code.help would be appreciated(in csharp)❔ Optional does not work with C# protobufsI have a proto buf with the timestamp field being marked as optional, but it doesn't seem to do anyt❔ ✅The largest inscribed circle inside concave (non-convex) polygon 2DHey, I want to make an algorithm that will return the largest inscribed circle in any 2D polygon (or