lidds Posted September 28, 2005 Posted September 28, 2005 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 Quote
Nate Bross Posted September 28, 2005 Posted September 28, 2005 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 Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Administrators PlausiblyDamp Posted September 28, 2005 Administrators Posted September 28, 2005 dim simon as string = "true" dim b as boolean b = boolean.Parse(simon) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
lidds Posted September 29, 2005 Author Posted September 29, 2005 Thanks a lot guys, someone else wrote the code and I've been asked to do a quick fix on it. Simon Quote
mskeel Posted September 29, 2005 Posted September 29, 2005 I beleive Convert.ToBoolean(simon) work too. Though I am unsure of the difference between that and Boolean.Parse(). Quote
Leaders Iceplug Posted October 4, 2005 Leaders Posted October 4, 2005 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. :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Leaders snarfblam Posted October 5, 2005 Leaders Posted October 5, 2005 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. Quote [sIGPIC]e[/sIGPIC]
lidds Posted October 5, 2005 Author Posted October 5, 2005 Thanks for the help guys, solved my problem. Cheers Quote
Console Posted February 17, 2006 Posted February 17, 2006 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 IfSimple may not always be the most sophisticated way of doing something, but it sure gets the job done :) (and did I mention language independent?) Quote
*Experts* Nerseus Posted February 18, 2006 *Experts* Posted February 18, 2006 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 Quote "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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.