patterncsharpMinor
Select the Enum to classify a string based on the first word found
Viewed 0 times
theenumclassifyfirstwordfoundbasedselectstring
Problem
I have written a small function that will check a string to see if it contains a certain word.
Depending on the word that is found first, the function will return the correct Enum value that needs to be used further down the line of the program.
Would there be a better way to check a string to see if it contains a certain Enum value?
The thing that gives me a little alarm bell in my head is that i am checking the same string 5 times in this function.
Enum
Function
Depending on the word that is found first, the function will return the correct Enum value that needs to be used further down the line of the program.
Would there be a better way to check a string to see if it contains a certain Enum value?
The thing that gives me a little alarm bell in my head is that i am checking the same string 5 times in this function.
Enum
public enum QuestionIdentifier
{
Wie,
Wat,
Waarom,
Wanneer,
Hoe,
Andere
}Function
private QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
if (vQTL.Contains("wie"))
{
return QuestionIdentifier.Wie;
}else if (vQTL.Contains("wat"))
{
return QuestionIdentifier.Wat;
}else if (vQTL.Contains("waarom"))
{
return QuestionIdentifier.Waarom;
}else if (vQTL.Contains("wanneer"))
{
return QuestionIdentifier.Wanneer;
}else if (vQTL.Contains("hoe"))
{
return QuestionIdentifier.Hoe;
}else
{
return QuestionIdentifier.Andere;
}
}Solution
You can utilise the
or with LINQ
Now you can add as much types as you want in your enum without even touching this method it will still work and find your newly created type.
Update
As @t3chb0t pointed in the comments there is a way to avoid calling
Enum class which can shorten your code significantly and it will be easier for future updatesprivate static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}or with LINQ
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}Now you can add as much types as you want in your enum without even touching this method it will still work and find your newly created type.
Update
As @t3chb0t pointed in the comments there is a way to avoid calling
.ToLower() on both the input string and the enum value by using the .IndexOf() overload which accepts different comparison types :private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName =
enumNames.FirstOrDefault(x => vQuestion.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
return enumValueName != null
? (QuestionIdentifier)Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}Code Snippets
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName =
enumNames.FirstOrDefault(x => vQuestion.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
return enumValueName != null
? (QuestionIdentifier)Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}Context
StackExchange Code Review Q#150860, answer score: 4
Revisions (0)
No revisions yet.