I need help converting a string to a Boolean value:

I've been trying to get the value (true or false) from the TopMost for my program and save it in my settings.

Settings1.Default["tm"] = ;Settings1.Default.Save();

The type for my setting 'tm' is a bool value (true, false),but I've only been using C# for a short amount of time and I'm not sure how to save whether or not my TopMost will be true or false.

Before you say to use the one in properties, it's a user option; I want them to be able to choose the option of whether it's on (true) or off (false), but have it save and load as a bool value.

4

Best Answer


You can convert a string to type Boolean by using any of the methods stated below:

 string sample = "True";bool myBool = bool.Parse(sample);// Orbool myBool = Convert.ToBoolean(sample);

bool.Parse expects one parameter which in this case is sample, .ToBoolean also expects one parameter.

You can use TryParse which is the same as Parse, but it doesn't throw any exception :)

string sample = "false";Boolean myBool;if (Boolean.TryParse(sample , out myBool)){// Do Something}

Please note that you cannot convert any type of string to type Boolean because the value of a Boolean can only be True or False

In C#, you can convert a string to a boolean using the bool.Parse() method or the Convert.ToBoolean() method. Both methods accept a string as input and return a boolean value.

For example, if you have a string variable called strValue that holds the value 'true', you can convert it to a boolean using bool.Parse(strValue) or Convert.ToBoolean(strValue).

It's important to note that these methods are case-insensitive, meaning that 'true', 'True', 'FALSE', and 'false' will all be converted correctly.

If the string cannot be converted to a boolean, an exception will be thrown. To handle this, you can use the bool.TryParse() method, which returns a boolean indicating whether the conversion succeeded or failed.

C# offers several ways to convert a string value to a boolean value. I will proceed to explain some of them below:

bool.Parse(string value) or System.Convert.ToBoolean(string value)

Both methods are quite similar in that they both take a string as their input value and return the boolean representation of that string as their output value. Note that both will throw a FormatException if the input string does not represent a boolean, whereas if the input string is null, bool.Parse will throw an ArgumentNullException while System.Convert.ToBoolean just returns false.

// Valid, also TRUE, FALSE, true, false, trUE, FAlse, etc. (case insensitive)bool result = bool.Parse("True");bool result = System.Convert.ToBoolean("False");// Invalidbool result = bool.Parse(null);bool result = System.Convert.ToBoolean("thisIsNotABoolean");

bool.TryParse(string value, out bool result)

Similar to bool.Parse except that it doesn't throw any exceptions directly, instead it returns a boolean value indicating whether or not the conversion could be performed. Also, the converted value now appears in an out bool result output parameter instead of being returned by the function.

bool success = bool.TryParse("True", out bool result); // success: Truebool success = bool.TryParse("False", out bool result); // success: Truebool success = bool.TryParse(null, out bool result); // success: Falsebool success = bool.TryParse("thisIsNotABoolean", out bool result); // success: False

string.Equals(string value)

This is not exactly a direct conversion method and I personally prefer any of the above, but if for some reason you don't have access to them, you can use this alternative.

bool result = "True" .Equals("true", StringComparison.OrdinalIgnoreCase); // Truebool result = "False".Equals("true", StringComparison.OrdinalIgnoreCase); // False

StringExtensions

Depending on what you want to achieve, an extension method might be a good option.

public static class StringExtensions{public static bool ToBoolean(this string value){if (bool.TryParse(value, out bool result)){return result;}return false;}}

Then

bool result = "True".ToBoolean();

If you just don't care about what to do when it fails, use this extension:

 public static bool GetBool(this string input){var boolResult = false;bool.TryParse(input, out boolResult);return boolResult;}

If it isn't True, true, it is false. No exceptions.