Netduino home hardware projects downloads community

Jump to content


Photo

Convert.ToBoolean


  • Please log in to reply
6 replies to this topic

#1 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 28 February 2012 - 09:31 PM

string test = "true"; bool test2 = (test.ToLower() == "true"); Since there is no Convert.ToBoolean, what is the solution?

#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 28 February 2012 - 09:40 PM

Found these but their not exactly perfect either:
bool.TrueString.Equals("true")
or this:
true.ToString().Equals("true")


#3 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 28 February 2012 - 10:23 PM

Found these but their not exactly perfect either:

bool.TrueString.Equals("true")
or this:
true.ToString().Equals("true")


Thank you for your reply. But I think both of those will always give me a true and they can't help evaluate a string type variable.

#4 Cabadam

Cabadam

    Advanced Member

  • Members
  • PipPipPip
  • 90 posts

Posted 29 February 2012 - 01:46 AM

Thank you for your reply. But I think both of those will always give me a true and they can't help evaluate a string type variable.


Could define your own "convert to boolean" method:
        public static bool ToBoolean(string str)
        {
            if (str == bool.TrueString)
            {
                return true;
            }
            
            if (str == bool.FalseString)
            {
                return false;
            }

            throw new InvalidOperationException("String was not a bool");
        }


#5 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1070 posts
  • LocationNorth Yorkshire, UK

Posted 29 February 2012 - 05:35 AM

string test = "true";

bool test2 = (test.ToLower() == "true");

Since there is no Convert.ToBoolean, what is the solution?

Is there a bool.Parse() method?

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#6 frabor

frabor

    New Member

  • Members
  • Pip
  • 7 posts

Posted 29 February 2012 - 03:59 PM

Could define your own "convert to boolean" method:

        public static bool ToBoolean(string str)
        {
            if (str == bool.TrueString)
            {
                return true;
            }
            
            if (str == bool.FalseString)
            {
                return false;
            }

            throw new InvalidOperationException("String was not a bool");
        }



There is a StackOverflow article that is worth reading about 'Why does Boolean.ToString output “True” and not “true”'. Great discussion.

For what I understood, the string has to be "True" and "False", and "true" and "false" are not valid.

http://stackoverflow...ue-and-not-true

#7 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 29 February 2012 - 08:00 PM

Thank you for your reply. But I think both of those will always give me a true and they can't help evaluate a string type variable.

Yes they will but I meant for you to replace the literal string "true" with the string variable you wish to test. Using your nomenclature it becomes like so:

string test = "true";

bool test2 = bool.TrueString.Equals(test);





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.