C
C#3y 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
sibber3y ago
no not really
Matt
MattOP3y 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
sibber3y ago
yeah definitely do that
bialasik__
bialasik__3y ago
@mcacutt maybe a tenary statement?
TheBoxyBear
TheBoxyBear3y 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
ero3y ago
(address, int size) = HLevel.CurrentLevelId switch works by the way just in case you wanna inline the size declaration
TheBoxyBear
TheBoxyBear3y ago
Or just turn the addresses into an array so no need for a switch
amio
amio3y ago
A dictionary?
TheBoxyBear
TheBoxyBear3y ago
address = HOpal.adresses[HLevel.CurrentLevelId]
amio
amio3y ago
Splitting "separate" bits of logic into methods with meaningful names should be done anyway, btw 😛
TheBoxyBear
TheBoxyBear3y 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
amio3y ago
Arrays "because they're numbers" is not automatically a good idea, it depends on what the keys are
TheBoxyBear
TheBoxyBear3y 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
amio3y ago
and if one of them is like int.maxvalue?
TheBoxyBear
TheBoxyBear3y ago
Then a dictionary of ids and addresses If the ids aren't sequential
amio
amio3y ago
So what I said, then.
TheBoxyBear
TheBoxyBear3y ago
But only if Dictionary works but might be overkill
amio
amio3y ago
No, the dictionary is not "overkill", what does that even mean
TheBoxyBear
TheBoxyBear3y ago
Might end up with duplicate sequential ids
amio
amio3y 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
Accord3y 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