locked
Convert.ToBoolean("Yes"); Give Errro : String was not recognized as a valid Boolean. RRS feed

  • Question

  • Hi all i am Very new in C#.

    i have follwing statement:

    Convert.ToBoolean("Yes");

    String was not recognized as a valid Boolean.
    How to String to Boolean?

    Saturday, November 7, 2009 9:15 AM

Answers

  • You can use Convert.ToBoolean("true") or Convert.ToBoolean("false")

    if you have values such as yes or no then you can use an if statement

    string s="Yes";
    bool result=false;
    if(s=="Yes")
    {
    result= true;
    }
    else
    {
    result=false;
    }
    • Proposed as answer by Tamer Oz Monday, November 9, 2009 4:15 AM
    • Marked as answer by Harry Zhu Friday, November 13, 2009 4:41 AM
    Saturday, November 7, 2009 9:23 AM

All replies

  • You can use Convert.ToBoolean("true") or Convert.ToBoolean("false")

    if you have values such as yes or no then you can use an if statement

    string s="Yes";
    bool result=false;
    if(s=="Yes")
    {
    result= true;
    }
    else
    {
    result=false;
    }
    • Proposed as answer by Tamer Oz Monday, November 9, 2009 4:15 AM
    • Marked as answer by Harry Zhu Friday, November 13, 2009 4:41 AM
    Saturday, November 7, 2009 9:23 AM
  • What about null?

    string s = "";

    bool? result = null;

    if(s == "Yes")

     result = true;

    elseif(s == "No")

     result = false;


    • Edited by pawelga Monday, April 23, 2012 6:44 AM
    Monday, April 23, 2012 6:43 AM