Update
Added constructor parameter for the dictionary to avoid lower/capital letter case problems as pointed in the comments.
There are 2 solutions:
First option would be to map string
-> InteractionType
like this:
private static readonly Dictionary<string, InteractionType> InteractionTypesDictionary = new Dictionary
<string, InteractionType>(StringComparer.CurrentCultureIgnoreCase)
{
[string.Empty] = InteractionType.None,
["default"] = InteractionType.None,
["gate"] = InteractionType.Gate,
["postit"] = InteractionType.Postit,
//...
};
Once you have all of your strings there your method will be just 1 line:
internal static InteractionType GetTypeFromString(string pType)
{
string key = pType.ToLower();
if(InteractionTypesDictionary.ContainsKey(key))
{
return InteractionTypesDictionary[key];
}
return InteractionType.None; // you might want to throw exception here.
}
The second option is the one I would prefer, I don't see any reason to not have the same string name as the enum
, it's easier to know what exact value you are looking for and you can use some of the predefined functions in the Enum
class. This will make your code just a few lines long:
internal static InteractionType GetTypeFromString(string pType)
{
string[] enumNames = Enum.GetNames(typeof(InteractionType));
string enumValueName = enumNames.FirstOrDefault(x => pType.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
if (enumValueName != null)
{
return (InteractionType) Enum.Parse(typeof(InteractionType), enumValueName);
}
return InteractionType.None;
}
This is the best solution because the previous 2 violate in some way the Open-Closed principle, because if you add some new value into the enum, you will need to modify some code somewhere in your project, but like this it will work even if you add new values, without updating this specific method. Also this is the shortest version, it simply requires you to have proper strings as parameters e.g
bb_blue_gate
InteractionType.Banzaigateblue
This is wrong and wont work it should be:
banzaigateblue // lower or capital letters dont matter
InteractionType.Banzaigateblue
Update
Added constructor parameter for the dictionary to avoid lower/capital letter case problems as pointed in the comments.
There are 2 solutions:
First option would be to map string
-> InteractionType
like this:
private static readonly Dictionary<string, InteractionType> InteractionTypesDictionary = new Dictionary
<string, InteractionType>(StringComparer.CurrentCultureIgnoreCase)
{
[string.Empty] = InteractionType.None,
["default"] = InteractionType.None,
["gate"] = InteractionType.Gate,
["postit"] = InteractionType.Postit,
//...
};
Once you have all of your strings there your method will be just 1 line:
internal static InteractionType GetTypeFromString(string pType)
{
string key = pType.ToLower();
if(InteractionTypesDictionary.ContainsKey(key))
{
return InteractionTypesDictionary[key];
}
return InteractionType.None; // you might want to throw exception here.
}
The second option is the one I would prefer, I don't see any reason to not have the same string name as the enum
, it's easier to know what exact value you are looking for and you can use some of the predefined functions in the Enum
class. This will make your code just a few lines long:
internal static InteractionType GetTypeFromString(string pType)
{
string[] enumNames = Enum.GetNames(typeof(InteractionType));
string enumValueName = enumNames.FirstOrDefault(x => pType.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
if (enumValueName != null)
{
return (InteractionType) Enum.Parse(typeof(InteractionType), enumValueName);
}
return InteractionType.None;
}
This is the best solution because the previous 2 violate in some way the Open-Closed principle, because if you add some new value into the enum, you will need to modify some code somewhere in your project, but like this it will work even if you add new values, without updating this specific method. Also this is the shortest version, it simply requires you to have proper strings as parameters e.g
bb_blue_gate
InteractionType.Banzaigateblue
This is wrong and wont work it should be:
banzaigateblue // lower or capital letters dont matter
InteractionType.Banzaigateblue
Update
Added constructor parameter for the dictionary to avoid lower/capital letter case problems as pointed in the comments.
There are 2 solutions:
First option would be to map string
-> InteractionType
like this:
private static readonly Dictionary<string, InteractionType> InteractionTypesDictionary = new Dictionary
<string, InteractionType>(StringComparer.CurrentCultureIgnoreCase)
{
[string.Empty] = InteractionType.None,
["default"] = InteractionType.None,
["gate"] = InteractionType.Gate,
["postit"] = InteractionType.Postit,
//...
};
Once you have all of your strings there your method will be just 1 line:
internal static InteractionType GetTypeFromString(string pType)
{
string key = pType.ToLower();
if(InteractionTypesDictionary.ContainsKey(key))
{
return InteractionTypesDictionary[key];
}
return InteractionType.None; // you might want to throw exception here.
}
The second option is the one I would prefer, I don't see any reason to not have the same string name as the enum
, it's easier to know what exact value you are looking for and you can use some of the predefined functions in the Enum
class. This will make your code just a few lines long:
internal static InteractionType GetTypeFromString(string pType)
{
string[] enumNames = Enum.GetNames(typeof(InteractionType));
string enumValueName = enumNames.FirstOrDefault(x => pType.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
if (enumValueName != null)
{
return (InteractionType) Enum.Parse(typeof(InteractionType), enumValueName);
}
return InteractionType.None;
}
This is the best solution because the previous 2 violate in some way the Open-Closed principle, because if you add some new value into the enum, you will need to modify some code somewhere in your project, but like this it will work even if you add new values, without updating this specific method. Also this is the shortest version, it simply requires you to have proper strings as parameters e.g
bb_blue_gate
InteractionType.Banzaigateblue
This is wrong and wont work it should be:
banzaigateblue // lower or capital letters dont matter
InteractionType.Banzaigateblue
Update
Added constructor parameter for the dictionary to avoid lower/capital letter case problems as pointed in the comments.
There are 2 solutions:
First option would be to map string
-> InteractionType
like this:
private static readonly Dictionary<string, InteractionType> InteractionTypesDictionary = new Dictionary
<string, InteractionType>(StringComparer.CurrentCultureIgnoreCase)
{
[string.Empty] = InteractionType.None,
["default"] = InteractionType.None,
["gate"] = InteractionType.Gate,
["postit"] = InteractionType.Postit,
//...
};
Once you have all of your strings there your method will be just 1 line:
internal static InteractionType GetTypeFromString(string pType)
{
string key = pType.ToLower();
if(InteractionTypesDictionary.ContainsKey(key))
{
return InteractionTypesDictionary[key];
}
return InteractionType.None; // you might want to throw exception here.
}
The second option is the one I would prefer, I don't see any reason to not have the same string name as the enum
, it's easier to know what exact value you are looking for and you can use some of the predefined functions in the Enum
class. This will make your code just a few lines long:
internal static InteractionType GetTypeFromString(string pType)
{
string[] enumNames = Enum.GetNames(typeof(InteractionType));
string enumValueName = enumNames.FirstOrDefault(x => pType.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
if (enumValueName != null)
{
return (InteractionType) Enum.Parse(typeof(InteractionType), enumValueName);
}
return InteractionType.None;
}
This is the best solution because the previous 2 violate in some way the Open-Closed principle, because if you add some new value into the enum, you will need to modify some code somewhere in your project, but like this it will work even if you add new values, without updating this specific method. Also this is the shortest version, it simply requires you to have proper strings as parameters e.g
bb_blue_gate
InteractionType.Banzaigateblue
This is wrong and wont work it should be:
banzaigateblue // lower or capital letters dont matter
InteractionType.Banzaigateblue
There are 2 solutions:
First option would be to map string
-> InteractionType
like this:
private static readonly Dictionary<string, InteractionType> InteractionTypesDictionary = new Dictionary
<string, InteractionType>
{
[string.Empty] = InteractionType.None,
["default"] = InteractionType.None,
["gate"] = InteractionType.Gate,
["postit"] = InteractionType.Postit,
//...
};
Once you have all of your strings there your method will be just 1 line:
internal static InteractionType GetTypeFromString(string pType)
{
string key = pType.ToLower();
if(InteractionTypesDictionary.ContainsKey(key))
{
return InteractionTypesDictionary[key];
}
return InteractionType.None; // you might want to throw exception here.
}
The second option is the one I would prefer, I don't see any reason to not have the same string name as the enum
, it's easier to know what exact value you are looking for and you can use some of the predefined functions in the Enum
class. This will make your code just a few lines long:
internal static InteractionType GetTypeFromString(string pType)
{
string[] enumNames = Enum.GetNames(typeof(InteractionType));
string enumValueName = enumNames.FirstOrDefault(x => pType.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
if (enumValueName != null)
{
return (InteractionType) Enum.Parse(typeof(InteractionType), enumValueName);
}
return InteractionType.None;
}
This is the best solution because the previous 2 violate in some way the Open-Closed principle, because if you add some new value into the enum, you will need to modify some code somewhere in your project, but like this it will work even if you add new values, without updating this specific method. Also this is the shortest version, it simply requires you to have proper strings as parameters e.g
bb_blue_gate
InteractionType.Banzaigateblue
This is wrong and wont work it should be:
banzaigateblue // lower or capital letters dont matter
InteractionType.Banzaigateblue
Update
Added constructor parameter for the dictionary to avoid lower/capital letter case problems as pointed in the comments.
There are 2 solutions:
First option would be to map string
-> InteractionType
like this:
private static readonly Dictionary<string, InteractionType> InteractionTypesDictionary = new Dictionary
<string, InteractionType>(StringComparer.CurrentCultureIgnoreCase)
{
[string.Empty] = InteractionType.None,
["default"] = InteractionType.None,
["gate"] = InteractionType.Gate,
["postit"] = InteractionType.Postit,
//...
};
Once you have all of your strings there your method will be just 1 line:
internal static InteractionType GetTypeFromString(string pType)
{
string key = pType.ToLower();
if(InteractionTypesDictionary.ContainsKey(key))
{
return InteractionTypesDictionary[key];
}
return InteractionType.None; // you might want to throw exception here.
}
The second option is the one I would prefer, I don't see any reason to not have the same string name as the enum
, it's easier to know what exact value you are looking for and you can use some of the predefined functions in the Enum
class. This will make your code just a few lines long:
internal static InteractionType GetTypeFromString(string pType)
{
string[] enumNames = Enum.GetNames(typeof(InteractionType));
string enumValueName = enumNames.FirstOrDefault(x => pType.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
if (enumValueName != null)
{
return (InteractionType) Enum.Parse(typeof(InteractionType), enumValueName);
}
return InteractionType.None;
}
This is the best solution because the previous 2 violate in some way the Open-Closed principle, because if you add some new value into the enum, you will need to modify some code somewhere in your project, but like this it will work even if you add new values, without updating this specific method. Also this is the shortest version, it simply requires you to have proper strings as parameters e.g
bb_blue_gate
InteractionType.Banzaigateblue
This is wrong and wont work it should be:
banzaigateblue // lower or capital letters dont matter
InteractionType.Banzaigateblue
There are 2 solutions:
First option would be to map string
-> InteractionType
like this:
private static readonly Dictionary<string, InteractionType> InteractionTypesDictionary = new Dictionary
<string, InteractionType>
{
[string.Empty] = InteractionType.None,
["default"] = InteractionType.None,
["gate"] = InteractionType.Gate,
["postit"] = InteractionType.Postit,
//...
};
Once you have all of your strings there your method will be just 1 line:
internal static InteractionType GetTypeFromString(string pType)
{
returnstring InteractionTypesDictionary[pTypekey = pType.ToLower()];;
if(InteractionTypesDictionary.ContainsKey(key))
{
return InteractionTypesDictionary[key];
}
return InteractionType.None; // you might want to throw exception here.
}
The second option is the one I would prefer, I don't see any reason to not have the same string name as the enum
, it's easier to know what exact value you are looking for and you can use some of the predefined functions in the Enum
class. This will make your code just a few lines long:
internal static InteractionType GetTypeFromString(string pType)
{
string[] enumNames = Enum.GetNames(typeof(InteractionType));
string enumValueName = enumNames.FirstOrDefault(x => pType.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
if (enumValueName != null)
{
return (InteractionType) Enum.Parse(typeof(InteractionType), enumValueName);
}
return InteractionType.None;
}
This is the best solution because the previous 2 violate in some way the Open-Closed principle, because if you add some new value into the enum, you will need to modify some code somewhere in your project, but like this it will work even if you add new values, without updating this specific method. Also this is the shortest version, it simply requires you to have proper strings as parameters e.g
bb_blue_gate
InteractionType.Banzaigateblue
This is wrong and wont work it should be:
banzaigateblue // lower or capital letters dont matter
InteractionType.Banzaigateblue
There are 2 solutions:
First option would be to map string
-> InteractionType
like this:
private static readonly Dictionary<string, InteractionType> InteractionTypesDictionary = new Dictionary
<string, InteractionType>
{
[string.Empty] = InteractionType.None,
["default"] = InteractionType.None,
["gate"] = InteractionType.Gate,
["postit"] = InteractionType.Postit,
//...
};
Once you have all of your strings there your method will be just 1 line:
internal static InteractionType GetTypeFromString(string pType)
{
return InteractionTypesDictionary[pType.ToLower()];
}
The second option is the one I would prefer, I don't see any reason to not have the same string name as the enum
, it's easier to know what exact value you are looking for and you can use some of the predefined functions in the Enum
class. This will make your code just a few lines long:
internal static InteractionType GetTypeFromString(string pType)
{
string[] enumNames = Enum.GetNames(typeof(InteractionType));
string enumValueName = enumNames.FirstOrDefault(x => pType.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
if (enumValueName != null)
{
return (InteractionType) Enum.Parse(typeof(InteractionType), enumValueName);
}
return InteractionType.None;
}
This is the best solution because the previous 2 violate in some way the Open-Closed principle, because if you add some new value into the enum, you will need to modify some code somewhere in your project, but like this it will work even if you add new values, without updating this specific method. Also this is the shortest version, it simply requires you to have proper strings as parameters e.g
bb_blue_gate
InteractionType.Banzaigateblue
This is wrong and wont work it should be:
banzaigateblue // lower or capital letters dont matter
InteractionType.Banzaigateblue
There are 2 solutions:
First option would be to map string
-> InteractionType
like this:
private static readonly Dictionary<string, InteractionType> InteractionTypesDictionary = new Dictionary
<string, InteractionType>
{
[string.Empty] = InteractionType.None,
["default"] = InteractionType.None,
["gate"] = InteractionType.Gate,
["postit"] = InteractionType.Postit,
//...
};
Once you have all of your strings there your method will be just 1 line:
internal static InteractionType GetTypeFromString(string pType)
{
string key = pType.ToLower();
if(InteractionTypesDictionary.ContainsKey(key))
{
return InteractionTypesDictionary[key];
}
return InteractionType.None; // you might want to throw exception here.
}
The second option is the one I would prefer, I don't see any reason to not have the same string name as the enum
, it's easier to know what exact value you are looking for and you can use some of the predefined functions in the Enum
class. This will make your code just a few lines long:
internal static InteractionType GetTypeFromString(string pType)
{
string[] enumNames = Enum.GetNames(typeof(InteractionType));
string enumValueName = enumNames.FirstOrDefault(x => pType.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
if (enumValueName != null)
{
return (InteractionType) Enum.Parse(typeof(InteractionType), enumValueName);
}
return InteractionType.None;
}
This is the best solution because the previous 2 violate in some way the Open-Closed principle, because if you add some new value into the enum, you will need to modify some code somewhere in your project, but like this it will work even if you add new values, without updating this specific method. Also this is the shortest version, it simply requires you to have proper strings as parameters e.g
bb_blue_gate
InteractionType.Banzaigateblue
This is wrong and wont work it should be:
banzaigateblue // lower or capital letters dont matter
InteractionType.Banzaigateblue