Forums

String.toBoolean?

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

String.toBoolean?

Postby Caligari » Sun Jul 05, 2009 7:46 pm

I have roughly the following lines in one of my programs:

Code: Select all
setting as String = "true"
option as bool = setting.toBoolean

And I'm getting a compile error: "string" does not contain a definition for "ToBoolean".

I also tried

Code: Select all
setting as String = "true"
option as bool = setting.toBool

But was told that: Cannot find a definition for "toBool" in "setting" whose type is "String". There is a member named "toBoolean" with a similar name.

Someone's telling fibs... :)

Previously I tried

Code: Select all
setting as String = "true"
option as bool = bool(setting)

And was told: Cannot find "bool". The same is true if I try "ToBoolean".

How should I be converting a string into a boolean?
Caligari
 
Posts: 33

Re: String.toBoolean?

Postby Charles » Sun Jul 05, 2009 11:27 pm

Short answer:

-- bool.parse(input)
-- bool.tryParse(input, out b)

Long answer:

If you type "C# convert string to boolean" in Google, one of the hits shows:
How can I convert string to boolean? ... Either Convert.ToBoolean, or Boolean.Parse

So I tried this:
class X

def main
for s in ['true', 'false', 'bad']
print
trace s
trace Convert.toBoolean(s)
trace bool.parse(s)

Which essentially works. An exception is thrown when s == 'bad' which inadvertently reveals that, at least on Mono 2.4.x, Convert.toBoolean is invoking Boolean.parse.

I don't know what your preferred style is, but I tend to use "type.parse(input)" such as "int.parse(input)" etc. The "Convert" class seems a little odd because it's not as if it could know about all types ahead of time such as System.Drawing.Point. Same thing with trying to put all "toXXX" methods on String, although I suppose extension methods would allow this.

I hope the "longer answer" shows how you can use "C# blah blah" in Google combined with "trace" to research issues.

-Chuck
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: String.toBoolean?

Postby hopscc » Sun Jul 05, 2009 11:36 pm

Straight .NET from Boolean or Convert classes

Code: Select all
      setting as String = "true"
      #option as bool = Convert.toBoolean(setting)
      option as bool = Boolean.parse(setting)
      assert option


Theres also success = Boolean.tryParse(setting, option). probably others as well

Dont think theres anything specifically cobra for this
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: String.toBoolean?

Postby hopscc » Sun Jul 05, 2009 11:46 pm

For such things re .Net I point a browser at http://msdn.microsoft.com/en-us/library/default.aspx
and type the guesses into the search bar... theres some addons that provide some sort of completion
but you usually get pages of hits into the API doc and occasional hits on MS rag articles blogs and wikis
Usually theres something that clarifies what you're after.

Works great for me and I'm not familiar at all with .Net
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: String.toBoolean?

Postby Charles » Sun Jul 05, 2009 11:51 pm

And if MSDN is running slow and heavy for you, see http://cobralang.blogspot.com/2009_05_09_archive.html.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 81 guests

cron