This question already has answers here:
Convert a string to an enum in C#
(29 answers)
Match string to enumeration?
(11 answers)
Closed 7 years ago.
I have my enum defined like this.
public enum Places : long
{
World = (long)1,
India = (long)23424848,
USA = (long)23424977
}
Now I get a string of value like 'India'. I want the corresponding value of the enumerator.
for instance if i get the string 'World'(or world - case insensitive ), I need the value 1 to be returned.
I tried this way:
long woeid = ((long)(typeof(Places)country));
this doesnot work.
Is there a simple way to do?
The method you want is Enum.Parse.
You would use it like this:
string country = "India";
Places myplace = (Places)Enum.Parse(typeof(Places), country);
long placeID = (long)Enum.Parse(typeof(Places), country);
Related
This question already has answers here:
Convert a string to an enum in C#
(29 answers)
How to get enum value by string or int
(13 answers)
Closed 1 year ago.
When I say Key, I'm referring to the "Keys" enum in Windows Forms. For instance:
I might have a string:
string key = "Q";
And I'm trying to convert it to this:
Keys.Q
How would I do this? (If even possible)
In case that the values of the string are not exactly the same as the enum you can do it with a switch-case statment.
Keys keys = key switch
{
"Q" => Keys.Q
...
};
If the values exactly the same just parse it:
public static TEnum ToEnum<TEnum>(this string strEnumValue)
{
if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
{
throw new Exception("Enum can't be parsed");
}
return (TEnum)Enum.Parse(typeof(TEnum), strEnumValue);
}
Keys keys = key.ToEnum<Keys>();
This question already has answers here:
Convert a string to an enum in C#
(29 answers)
Deserialize Json string to Enum C#
(2 answers)
Closed 3 years ago.
I would like to know if it is possible to convert an enum key to a string and a string to an enum key like follow:
enum AllGameSettingsLabels {
Options_Controls_Joystick_Sensitivity,
Options_Display_Screen_Resolution,
Game_Character_Name
// ...
}
IDictionary<AllGameSettingsLabels, string> settings = new Dictionary<AllGameSettingsLabels, string>();
settings[AllGameSettingsLabels.Parse(AllGameSettingsLabels.Joystick_Sensitivity.ToString() + "_Player_1")] = (0.25).ToString();
I was trying to come up with an easy way to manage all the game settings and later converting them to JSON.
This question already has answers here:
How do i split a String into multiple values?
(5 answers)
Closed 3 years ago.
Not entirely sure the following code is going to help many people, but here goes
try
{
uvConnect = UniObjects.OpenSession(serverId, sUser, sPass, sAcct, "uvcs");
// Open Movie File
UniFile uvFile = uvConnect.CreateUniFile("MOVIES");
UniDynArray movieRec = uvFile.Read(txtMovieId.Text);
string sMovieData = movieRec.StringValue;
MessageBox.Show(sMovieData);
}
sMovieData contains a single string of the entire record retrieve from MOVIES file, each field is deliminated by a char(253) character in the database I am using.
Is there a function/method/etc to convert the string to an array using char(253) as a value deliminator
Something like this should work:
string[] fields = sMovieData.Split((char)253);
Try this... string[] arrayValues = "stringToConvertToArray".Split((char)253);
This question already has answers here:
What's the use/meaning of the # character in variable names in C#?
(9 answers)
How do I use a C# keyword as a property name?
(1 answer)
C# keywords as a variable
(4 answers)
Closed 3 years ago.
How can I declare a variable with name "operator"?
public string operator;
you can use any reserved word by prefixing the name of your identifier with an # : #operator
var #operator = "+";
var #event = new { name = "Burning man" };
var #var = 23;
var #enum = new List { 1, 2, 3 };
needless to say, this is not so much helping readability, but if you feel it fits your case, you can use it.
Use public string #operator.
The # prefix allows you to use reserved words as variable names.
This question already has answers here:
Can I convert a C# string value to an escaped string literal?
(16 answers)
Closed 9 years ago.
Is there a C# method like Python's repr() to get the true representation of the object? Suppose we have:
string identifier = "22\n44";
Console.WriteLine(identifier);
This would return
22
44
Is there a way to get
"22\n44"
In Python this is easy. We can just do repr("22\n44").
I thought of this question because I was trying to convert "2244" to '2244' using
var identifier = "2244";
identifier = identifier.Replace("\"", "'");
Console.WriteLine(identifier);
The output is just 2244, because double quotes are for our purpose. But in my case, I did this to get what I wanted:
identifier = string.Format("'{0}'", identifier);
because initially the database was receiving the query as IN ("2244") instead of IN ('2244') and was throwing Invalid Number error.
string identifier = "22\n44";
Console.WriteLine("\"{0}\"", identifier.Replace("\n", #"\n"));