I'm developing a poker app and currently I want to store all the card combinations names I'm planning to use list and do something like this :
private static List<string> combinationNames = new List<string>
{
" High Card ",
" Pair ",
" Two Pair ",
" Three of a Kind ",
" Straight ",
" Flush ",
" Full House ",
" Four of a Kind ",
" Straight Flush ",
" Royal Flush ! "
};
for (int j = 0; j < combinationNames.Count; j++)
{
if (current == j)
{
MessageBox.Show("You Have : ", combinationNames[j]);
}
}
So is there a better way of storing those names and later access them like I did ?
There's not much to go on in your question to understand what's specifically wrong with the code you have. That said, at the very least I would expect the following to be an improvement:
private readonly static string[] combinationNames =
{
" High Card ",
" Pair ",
" Two Pair ",
" Three of a Kind ",
" Straight ",
" Flush ",
" Full House ",
" Four of a Kind ",
" Straight Flush ",
" Royal Flush ! "
};
if (current >= 0 && current < combinationNames.Length)
{
MessageBox.Show("You Have : ", combinationNames[current]);
}
I.e.:
readonly
j
is compare it to current
; there's no need to enumerate every possible value for j
…just make sure current
is within the valid range and then use its value directly.Note on that last point it's not really clear where you get current
from, but likely it should already be guaranteed to be valid before you get as far as displaying the text, so you shouldn't even really need the range check. I just put that there to ensure the new version of code above is reasonably consistent with the behavior of the code you showed (what little was there).
If you need more specific advice than the above, please explain more precisely what you think would be "better" and in what way the code you have now is not sufficiently addressing your needs. I.e. what does the code do now and how is that different from what you want it to do?