Reputation: 43
Problem:
I can return the string representation of a single valued enum
with enum.ToString()
. In the cases of multi-valued enum
(e.g.: signified using the OR
operator |
), all I get back in the enum.ToString()
call is the integer value because the enum
fails to parse
.
Work-arounds/Hindrances:
I can't create multiple instances of the same value in enum
, such as:
public enum Languages: int
{
English = 0, // From American English
English = 1, // From UK English
Spanish = 2, // From Castillian Spanish
Spanish = 3, // From Latin American Spanish
Portuguese = 4 // From Brasilian Portuguese
Portuguese = 5, // From European Portuguese
}
as this violates the standard and the IDE gets super frowny-face about that.
Example of Problem:
public enum Languages: int
{
English = 0 | 1, // American or UK
Spanish = 2 | 3, // Castillian or Latin American
Portuguese = 4 | 5, // Brasil or Portugal
MyMadeUpLanguage = 6,
}
int selectedLanguage = 1;
Languages result = (Languages)Enum.Parse(typeof(Languages), selectedLanguage);
string resultString = result.ToString(); // This is where '1' is returned and not English.
int nextSelectedLanguage = 6;
Languages differentResult = (Languages)Enum.Parse(typeof(Languages), nextSelectedLanguage);
string nextResultString = differentResult.ToString(); // This returns 'MyMadeUpLanguage', as we would expect.
Expectations (or what I'm trying to do):
What I'm expecting is that the conditional should match, so 0
or 1
would match English
and my enum result would then be Languages.English
and, so, the string returned would English
and not 1
.
Instead, what seems to happen is that the call to parse
the enum
just returns as an int
and not a Language
type (referencing the example above), so - of course - the enum.ToString()
method returns an int
.
For multi-valued enum
, where it's possibe that 2
to x <= int.MaxValue
values will suffice for the enum
, how can I get the call to parse
the enum
to cast successfully to the expected enum
(which would, resultantly, bring the proper string back in the return of the enum.ToString()
method)?
Upvotes: 0
Views: 164
Reputation: 186698
I suggest using Flags
attribute, e.g.:
[Flags]
public enum Languages: int
{
None = 0, // None
English = 1 << 30, // Languages
Spanish = 1 << 29,
Portuguese = 1 << 28,
MyMadeUpLanguage = 1 << 27,
// Dialects
EnglishUS = English | 1, // From American English
EnglishGB = English | 2, // From UK English
EnglishCA = English | 3, // From Canada English
SpanishES = Spanish | 1, // From Castillian Spanish
SpanishLA = Spanish | 2, // From Latin American Spanish
PortugueseBR = Portuguese | 1, // From Brasilian Portuguese
PortuguesePT = Portuguese | 2, // From European Portuguese
MyMadeUpLanguageDialect1 = MyMadeUpLanguage | 1
}
Typical comparisons:
Languages selectedLanguage ...
if (Languages.EnglishGB & Languages == Languages.EnglishGB) {
// From Exactly English Great Britain
...
}
if (Languages.English & Languages == Languages.English) {
// From some kind of English (Great Britain, or US dialect)
...
}
Upvotes: 1