C
C#2y ago
Matt

❔ alternative to switch statement or if statements?

is there something better I can use in this situation?
int size = 300;
switch (HLevel.CurrentLevelId)
{
case 0:
address = HOpal.address1;
size = 25;
break;
case 10:
address = HOpal.address2;
break;
default:
address = HOpal.address3;
}
int size = 300;
switch (HLevel.CurrentLevelId)
{
case 0:
address = HOpal.address1;
size = 25;
break;
case 10:
address = HOpal.address2;
break;
default:
address = HOpal.address3;
}
21 Replies
sibber
sibber2y ago
no not really
Matt
Matt2y ago
sad. It's kinda a gross and obnoxiously long statement. Maybe I'll just extract it to its own function to reduce the size of the method it's in
sibber
sibber2y ago
yeah definitely do that
bialasik__
bialasik__2y ago
@mcacutt maybe a tenary statement?
TheBoxyBear
TheBoxyBear2y ago
Not really if you assign multiple values and three cases can get messy Also not the best because of the size assignment but you could do
(adress, size) = HLevel.CurrentLevelId swutch
{
someValue => (adressVal, sizeVal),
...
_ => // default case
};
(adress, size) = HLevel.CurrentLevelId swutch
{
someValue => (adressVal, sizeVal),
...
_ => // default case
};
ero
ero2y ago
(address, int size) = HLevel.CurrentLevelId switch works by the way just in case you wanna inline the size declaration
TheBoxyBear
TheBoxyBear2y ago
Or just turn the addresses into an array so no need for a switch
amio
amio2y ago
A dictionary?
TheBoxyBear
TheBoxyBear2y ago
address = HOpal.adresses[HLevel.CurrentLevelId]
amio
amio2y ago
Splitting "separate" bits of logic into methods with meaningful names should be done anyway, btw 😛
TheBoxyBear
TheBoxyBear2y ago
just an array, the keys, in this case being member names already follow a number sequence and add an if special case for size on 0
amio
amio2y ago
Arrays "because they're numbers" is not automatically a good idea, it depends on what the keys are
TheBoxyBear
TheBoxyBear2y ago
Unless there are cases there some levels use the same address Or there's a huge number of levels with a lot of overlap to not justify an huge array
amio
amio2y ago
and if one of them is like int.maxvalue?
TheBoxyBear
TheBoxyBear2y ago
Then a dictionary of ids and addresses If the ids aren't sequential
amio
amio2y ago
So what I said, then.
TheBoxyBear
TheBoxyBear2y ago
But only if Dictionary works but might be overkill
amio
amio2y ago
No, the dictionary is not "overkill", what does that even mean
TheBoxyBear
TheBoxyBear2y ago
Might end up with duplicate sequential ids
amio
amio2y ago
The dictionary is the data structure of choice when you're mapping stuff from one thing to another and there's a simple equivalence. It's an ID. Anyway, amio out
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