C
C#4mo ago
Jason_Bjorn

Best way to set all applicable flags?

I have a string that represent possible flags. How can I set all the flags that are in the string?
Classifiers getClassifiers(string s) {
Classifiers c = new();
foreach (var enumVal in Enum.GetValues(typeof(Classifiers)))
{
if (s.Contains(enumVal.ToString())) c += (int)enumVal;
}

return c;
}

Classifiers c = getClassifiers("CTR HI LAG");
Console.WriteLine(c); // HI, CTR, LAG

[Flags]
enum Classifiers
{
LO = 1<<0,
HI = 1<<1,
LFT = 1<<2,
RIT = 1<<3,
CTR = 1<<4,
LAG = 1<<5
}
Classifiers getClassifiers(string s) {
Classifiers c = new();
foreach (var enumVal in Enum.GetValues(typeof(Classifiers)))
{
if (s.Contains(enumVal.ToString())) c += (int)enumVal;
}

return c;
}

Classifiers c = getClassifiers("CTR HI LAG");
Console.WriteLine(c); // HI, CTR, LAG

[Flags]
enum Classifiers
{
LO = 1<<0,
HI = 1<<1,
LFT = 1<<2,
RIT = 1<<3,
CTR = 1<<4,
LAG = 1<<5
}
This works but is there a better way?
9 Replies
reflectronic
reflectronic4mo ago
can you change the format of the string Enum.Parse<Classifiers>("CTR,HI,LAG")
Jason_Bjorn
Jason_BjornOP4mo ago
I supposed I could change the format of the string, It might also include other information not needed by the classifier though
Anton
Anton4mo ago
what's the input look like?
FusedQyou
FusedQyou4mo ago
This is horrible, why would you do this? If you want to set all the flags, just apply them normally. Classifiers c = LO | HI | LFT | RIT | CTR | LAG; People also usually add a flag to the enum that specifies groups of flags, or all flags.
[Flags]
enum Classifiers
{
LO = 1<<0,
HI = 1<<1,
LFT = 1<<2,
RIT = 1<<3,
CTR = 1<<4,
LAG = 1<<5,
ALL = LO | HI | LFT | RIT | CTR | LAG,
}
[Flags]
enum Classifiers
{
LO = 1<<0,
HI = 1<<1,
LFT = 1<<2,
RIT = 1<<3,
CTR = 1<<4,
LAG = 1<<5,
ALL = LO | HI | LFT | RIT | CTR | LAG,
}
Doing weird string parsing is unnecessary complication and just straight up confusing. This is also a very poor and slow solution. Also, this is your solution regardless if you remove this parsing system. You can then pass "ALL" to the string and parse all of them.
Anton
Anton4mo ago
I suspect they are trying to parse it out in some larger piece of text
cned
cned4mo ago
string.Contains is, 90% of a code smell to me. It's almost never correct, since a lot of text "cotains" substrings unexpectedly. Like in this example, "LO" and "HI" are going to show up in a LOT of strings. Like is "CTRIT" supposed to be both "CTR" an "RIT"? I strongly suspect no. And I've seen it used for things like ".Contains(googl.com)", or the like in URL's. But "my.site.evil/#google.com" contains it. You should already know what piece of string you are looking at is supposed to contain, then it either "is" that or not. No contains.
FusedQyou
FusedQyou4mo ago
I assume that the whole point is to have a concatenated string of the flags, meaning that it doesn't contain bloat like other text Not that it matters, it's still horrible and inefficient. I doubt they are familiar with how to use flags properly
You should already know what piece of string you are looking at is supposed to contain, then it either "is" that or not. No contains.
Also this, it doesn't really matter. They should not use a string, period.
Jason_Bjorn
Jason_BjornOP4mo ago
This is how I get the json I didn't chose to put it all in a string with a bunch of other random crap
FusedQyou
FusedQyou4mo ago
What is this JSON? Can you change how it returns the data? Then just return an integer that represents the flag This still is excessive overcomplication. If you get this JSON from somebody else then they really need to improve it.

Did you find this page helpful?