473,327 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

can I avoid the evail to convert from string to a boolean

Hi,
Is there a better way to do this?
The problem that I have is that x can be "true" or "false" (as type
string), or true or false (as boolean type). I'm doing comparisons
like...

if (eval(x) == true){
// do something...
}

or..

if (eval(x) == false){
// do something...
}

I'd like to get rid of the eval, I tried to use the !!, but if I do !!x
when x is "false", it returns true since it's not an empty string. Do
you know a better way to do this? Without forcing x to be always a
string or always a boolean?

Thanks,
Carlos

Oct 6 '06 #1
2 1993

Carlos Aguayo wrote:
Hi,
Is there a better way to do this?
The problem that I have is that x can be "true" or "false" (as type
string), or true or false (as boolean type). I'm doing comparisons
like...

if (eval(x) == true){
// do something...
}

or..

if (eval(x) == false){
// do something...
}

I'd like to get rid of the eval, I tried to use the !!, but if I do !!x
when x is "false", it returns true since it's not an empty string. Do
you know a better way to do this? Without forcing x to be always a
string or always a boolean?
Though it's redundant to do so on a string, you can do the following:

if(x.toString() == "true")
{
//etc.
}

[...]

if(x.toString() == "false")
{
//etc.
}

This will work for both booleans and strings.

Oct 6 '06 #2
JRS: In article <11**********************@h48g2000cwc.googlegroups .com>,
dated Fri, 6 Oct 2006 16:49:42 remote, seen in
news:comp.lang.javascript, Carlos Aguayo <ca***********@gmail.com>
posted :
>Is there a better way to do this?
Yes.
>The problem that I have is that x can be "true" or "false" (as type
string), or true or false (as boolean type).
Within a program, away from I/O, a logical value with exactly two
significantly-different states should be a Boolean, and not a String.

You should convert the value to genuine Boolean as soon as it is
generated, if it is not already Boolean. The type will be known at
creation.

You should not ask your users to put "true" or "false" in a control that
inputs strings; you should use a checkbox instead.

If you are obliged to accept such strings, then convert to lower case,
compare with both "true" and "false", and decide what to do if neither
match.

It's a good idea to read the newsgroup and its FAQ. See below.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bradley Plett | last post by:
I have what should be a trivial problem. I am using XMLSerializer to serialize an object. It serializes boolean values as "True" and "False". I then want to use an XSLT on this XML, and I want...
3
by: ET | last post by:
I don't know whats the problem, but after I added functions to first verify, then relink linked tables if not found, now I can't convert that database to MDE format. I can split the database, but...
6
by: Tim Cartwright | last post by:
I have a page that has the login control on it, nothing else. This page inherits from a master page, neither page has any code in it. This page works perfectly when running on the WebDev debug web...
14
by: Me | last post by:
Hi all I am getting a really bizzare error on when I convert a string into a datetime: The code is : DateTime dt1 = Convert.ToDateTime("10 Sep 2005"); Console.WriteLine(dt1.Year);
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
4
by: simonZ | last post by:
Why this don't work: Boolean test; String testValue; testValue="0"; test=System.Convert.ToBoolean(testValue); How can I convert string to boolean?
0
by: halex | last post by:
Hello, I am having deadlock problem when I have a lot of visitors on my website at the same time. I am using NetTiers templates to generate C# classes for accessing DB layer and problem is in my...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
7
by: =?Utf-8?B?SmFzb24=?= | last post by:
Hello, can someone please suggest to me how I can keep my ftp connection from timing out after a large file download? I am using SSIS and I have tried multiple ways of ftp'ing the files in my...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.