Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to boolean in C#

Tags:

c#

boolean

I need help converting a string to a bool 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.

like image 581
Whiteout Productions Avatar asked Mar 31 '18 17:03

Whiteout Productions


People also ask

How do I convert string to boolean?

To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Can we convert string to boolean in C#?

OP, you can convert a string to type Boolean by using any of the methods stated below: string sample = "True"; bool myBool = bool. Parse(sample); // Or bool myBool = Convert. ToBoolean(sample);

Can strings be boolean?

We can convert String to boolean in java using Boolean. parseBoolean(string) method. To convert String into Boolean object, we can use Boolean.

Can a string be boolean in C++?

Given a single-digit string in C++, convert it into a corresponding boolean value, i.e., if the string is 1, the corresponding boolean value should be true, and if the string is 0, the corresponding boolean value should be false.


3 Answers

I know this is not an ideal question to answer but as the OP seems to be a beginner, I'd love to share some basic knowledge with him... Hope everybody understands

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

 string sample = "True";
 bool myBool = bool.Parse(sample);

 // Or

 bool 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

Hope you understand :)

like image 173
Christopher H. Avatar answered Oct 21 '22 00:10

Christopher H.


You must use some of the C # conversion systems:

string to boolean: True to true

string str = "True";
bool mybool = System.Convert.ToBoolean(str);

boolean to string: true to True

bool mybool = true;
string str = System.Convert.ToString(mybool);

//or

string str = mybool.ToString();

bool.Parse expects one parameter which in this case is str, even .

Convert.ToBoolean expects one parameter.

bool.TryParse expects two parameters, one entry (str) and one out (result).

If TryParse is true, then the conversion was correct, otherwise an error occurred

string str = "True";
bool MyBool = bool.Parse(str);

//Or

string str = "True";
if(bool.TryParse(str, out bool result))
{
   //Correct conversion
}
else
{
     //Incorrect, an error has occurred
}
like image 29
Héctor Manuel Martínez Durán Avatar answered Oct 21 '22 02:10

Héctor Manuel Martínez Durán


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.

like image 1
Patrick Knott Avatar answered Oct 21 '22 00:10

Patrick Knott