John Vasquez
John Vasquez

Reputation: 115

How to convert from string over to bool

I have the following code, that when run, saves a string value True as seen here: enter image description here

Here is the code:

currentSetting = AppSession.Repository.Settings.Find(SettingQuery.ID == new ID("IsUsersLockedOut"));
            currentSetting[0].Value = true.ToString(); 
            AppSession.Repository.Settings.Save(currentSetting[0]);

My question today is how would I be able to get a boolean value back instead of the string value, the expected result should be when saved, the value == 1, instead of a string true in the picture above. Thank you all.

Upvotes: 0

Views: 1180

Answers (1)

Caius Jard
Caius Jard

Reputation: 74700

Loads of ways to do this, here are a few:

    string boo = "true";

    bool b1 = Convert.ToBoolean(boo);
    bool b2 = bool.Parse(boo);
    bool b3 = bool.TryParse(boo, out var bParsed) && bParsed;
    bool b4 = boo.Equals("true", StringComparison.OrdinalIgnoreCase);

The first two will explode if a non bool is passed. The third returns true only if something parsably boolean was passed otherwise false, and the fourth returns true only if some version of the word TruE was passed otherwise false

Upvotes: 2

Related Questions