C
Join ServerC#
help
❔ alternative to switch statement or if statements?
Mmcacutt11/17/2022
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;
}
CCyberrex11/17/2022
no not really
Mmcacutt11/17/2022
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
CCyberrex11/17/2022
yeah definitely do that
Bbialasik__11/17/2022
@mcacutt maybe a tenary statement?
TTheBoxyBear11/17/2022
Not really if you assign multiple values and three cases can get messy
TTheBoxyBear11/17/2022
Also not the best because of the size assignment but you could do
(adress, size) = HLevel.CurrentLevelId swutch
{
someValue => (adressVal, sizeVal),
...
_ => // default case
};
EEro11/17/2022
(address, int size) = HLevel.CurrentLevelId switch
works by the wayEEro11/17/2022
just in case you wanna inline the size declaration
TTheBoxyBear11/17/2022
Or just turn the addresses into an array so no need for a switch
Aamio11/17/2022
A dictionary?
TTheBoxyBear11/17/2022
address = HOpal.adresses[HLevel.CurrentLevelId]
Aamio11/17/2022
Splitting "separate" bits of logic into methods with meaningful names should be done anyway, btw 😛
TTheBoxyBear11/17/2022
just an array, the keys, in this case being member names already follow a number sequence
TTheBoxyBear11/17/2022
and add an if special case for size on 0
Aamio11/17/2022
Arrays "because they're numbers" is not automatically a good idea, it depends on what the keys are
TTheBoxyBear11/17/2022
Unless there are cases there some levels use the same address
TTheBoxyBear11/17/2022
Or there's a huge number of levels with a lot of overlap to not justify an huge array
Aamio11/17/2022
and if one of them is like int.maxvalue?
TTheBoxyBear11/17/2022
Then a dictionary of ids and addresses
TTheBoxyBear11/17/2022
If the ids aren't sequential
Aamio11/17/2022
So what I said, then.
TTheBoxyBear11/17/2022
But only if
TTheBoxyBear11/17/2022
Dictionary works but might be overkill
Aamio11/17/2022
No, the dictionary is not "overkill", what does that even mean
TTheBoxyBear11/17/2022
Might end up with duplicate sequential ids
Aamio11/17/2022
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.
Aamio11/17/2022
Anyway, amio out
AAccord11/22/2022
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.