Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Is it possible to turn a string variable into a boolean variable

 

e.g:

 

dim simon as string = "true"
simon.toboolean()

 

Just an example.

 

Thanks in advance

 

Simon

Posted

I would just use a boolean from the begining, if that is beond your control I'd write a function like this.

 

'Untested Code but you get the idea.
Public Function IsTrue(sInput as String) as Boolean
       If lcase(sInput) = "true" Then
               Return True
       ElseIf lcase(sInput) = "false" Then
               Return False
       End If
End Function

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • Leaders
Posted
Boolean.Parse specifically converts strings into Boolean values. Convert.ToBoolean converts anything into Boolean... so its flexible but Convert has to figure out that you are using a string. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • Leaders
Posted

Sorry, I guess I just like to split hairs. I wouldn't say that Convert really has to figure out that you are using a string. The compiler chooses the correct overload at compile time, so that essentially Convert.ToBoolean(String) is the same as Boolean.Parse(String).

 

There might be differences, for instance, localization, between the Parse methods and the Convert class. That, I'm not sure about.

[sIGPIC]e[/sIGPIC]
  • 4 months later...
Posted

The quick and simple solution (without needing to use or test any conversion methods) would be:

 

Dim simon As String = "true"
Dim b As Boolean

If simon = "true" Then
   b = True
Else
   b = False
End If

Simple may not always be the most sophisticated way of doing something, but it sure gets the job done :) (and did I mention language independent?)

  • *Experts*
Posted

I went digging into Reflector and found out a few things...

 

Boolean.Parse() will be slightly faster than Convert.ToBoolean because the current implementation of Convert.ToBoolean(String) calls Boolean.Parse().

 

Of note: Convert.ToBoolean(String) will return a "default" of false if you pass in null (Nothing in VB). A call to Boolean.Parse(null) will throw an exception.

 

If you know 100% that the string will always be "true" or "false" (and not "True" or "1" or "-1" or...), I'd find it Ok to go with the direct compare.

 

I actually had to write my own "IsBoolean(string)" function to handle some "odd" values, coming from a 3rd party. It had to interpret true/false, yes/no, 1/0, and a bunch of other values.

 

I didn't really have much to add, but I had already typed this all in... :)

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...