...

View Full Version : Resolved convert int to bool



alykins
12-27-2011, 06:05 PM
How do I do this equivalent function in javascript?


int x = 0;
int y = 1;

convertBool(x);
convertBool(y);

private void convertBool(obj)
{
bool rtnB = Convert.ToBoolean(obj);
string TorF = (!obj) ? "true" : "false" ;
Console.WriteLine(TorF);
}

// code would output
// false
// true


I am trying to pass a bit by my function call


function fxn(obj){
document.getElementById('box').style.display = (!obj) 'block' : 'none' ;
}

code somewhere else...
onmouseover="fxn(1);" onmouseout="fxn(0)"

I know I could do it via if obj != 0 or if obj == 0 but I want to be able to convert the bool- I know how to do it other ways but if js does not support a "one liner" I will do it via int compare

_Aerospace_Eng_
12-27-2011, 06:23 PM
You don't have to convert it to a boolean as javascript already allows you to mix variables and types (aside from arrays).

This will work

function fxn(obj){
document.getElementById('box').style.display = (!obj) ? 'block' : 'none' ;
}

alykins
12-27-2011, 06:34 PM
I tried that exact line of code earlier (sry in original post I was just retyping it and missed the "?") and it spazed out... for now I have changed it to (obj > 0) to just use the int- i don't get why original code didn't work... it just stuck my "block" there

html


<div id="MasterWrapper">
<div id="linx" onmouseover="popup(1);" onmouseout="popup(0);">
<div id="pop"></div>
</div>
</div>

and some CSS


#MasterWrapper
{
margin:30px auto;
border: 3px ridge white;
width:800px;
height:500px;
overflow:auto;
}
#linx
{
width:4px;
height:500px;
float:left;
background:green;
position:relative;
z-index:0;
}
#pop
{
position:absolute;
z-index:1;
width:100px;
height:500px;
background:orange;
display:none;
}


and then the two pieces of code in question...
*working*


function popup(obj) {
document.getElementById('pop').style.display = (obj > 0) ? 'block' : 'none';
}


and the original which as you, Aerospace already said (I thought js handled implicit conv)


function popup(obj) {
document.getElementById('pop').style.display = (!obj) ? 'block' : 'none';
}

does not work... it gets the orange box stuck in the corner and then it never goes away until ironically you mouseover (sending it a 1 so it should be true -> 'block') it goes away and then when mouseout it comes back again ... ???

_Aerospace_Eng_
12-27-2011, 06:45 PM
I think it is working but is just going to negate what you give it e.g. if you give it 1, it becomes 0, if you give it 0, it becomes 1.
So for example if you called

popup(1); // #pop stays at display='none'
if you called

popup(0); // #pop goes to display='block'
If you want it the other way then use this

function popup(obj) {
document.getElementById('pop').style.display = (obj) ? 'block' : 'none';
}
And keep your html the same. If you want to keep your function as is then you need to change your html to this.

onmouseover="popup(0);" onmouseout="popup(1);"

alykins
12-27-2011, 07:35 PM
hmmm... idk i am really lost as to why it is/isn't working... for "the heck of it" I fiddled with the obj/!obj and 0/1 sending combo... the code works either way with onmouse(0) and mouseout(1) with either (obj) or (!obj) ... I am wondering if the 1 is throwing it as null- for some reason VS keeps telling me that it is not a valid place to break so... anyways not the biggest deal ever- for now i changed it to (obj) and mouseover(0) and mouseout(1) even though "both" work just to keep it logic wise correct... if anyone else reaqds and knows "what the issue is" let me know otherwise I am marking resolved

Logic Ali
12-27-2011, 08:52 PM
and then the two pieces of code in question...
*working*

function popup(obj) {
document.getElementById('pop').style.display = (obj > 0) ? 'block' : 'none';
}

and the original which as you, Aerospace already said (I thought js handled implicit conv)



function popup(obj) {
document.getElementById('pop').style.display = (!obj) ? 'block' : 'none';
}

does not work...


(obj > 0) != (!obj)

alykins
12-27-2011, 08:58 PM
(obj > 0) != (!obj)

well aware... (obj > 0) is treating obj as an int which is passed via fxn call; (!obj) is on the assumption that js will implicitly convert the datatype (presumably either 0|1) to false|true respectively and throw a js debug error if value other than 0|1 is passed.

_Aerospace_Eng_
12-27-2011, 10:54 PM
I've told you what the issue was. You originally had

onmouseover="popup(1);" onmouseout="popup(0);"
With your original function it would go like this.
onmouseover

function popup(1) {
document.getElementById('pop').style.display = (!1) ? 'block' : 'none'; // so now !1 becomes 0 hence why you don't get anything to show up.
}
onmouseout

function popup(1) {
document.getElementById('pop').style.display = (!0) ? 'block' : 'none'; // so now !0 becomes 1 hence why it does work.
}
So with what I told you changing your function to get rid of the ! and then using your current html, would work or keeping your function but pass in a 0 for onmouseover and a 1 for onmouseout it would work. Honestly though you don't need javascript for any of this. You can use CSS

#linx:hover #pop {
display:block;
}

Old Pedant
12-27-2011, 11:58 PM
Not to ask a really really dumb question, but...

WHY are you passing something OTHER than boolean, in the first place???



... onmouseover="popup(true);" onmouseout="popup(false);" ...


and then


function popup(yesno)
{
document.getElementById('pop').style.display = yesno ? 'block' : 'none';
}


Why would you *want* to use 1 and 0 or any other values, in place of true/false???

Mystifies me utterly.

alykins
12-28-2011, 02:08 AM
@OldPedant because I am used to C# and in C# you can do that no prob bc there is a convert method- and there are times to use Convert.ToBoolean(int) hence why it is there-

before there are ten posts about how this is not C# it is js I know that but I made my code based on that logic hence


How do I do this equivalent function in javascript?


the short and simple answer is there is no equiv js "one line" conversion of int to bool but what I thought Aerospace was saying was that it did it automatically... his statement of obj would still be 1 "...(!1)..." means that it does not automatically convert- i misread his original answer

_Aerospace_Eng_
12-28-2011, 05:43 AM
Well even !true = false and !false = true. Do you see the negation going on there? 1 and 0 can be booleans but yes just use true or false or as I stated don't even use javascript for this.

Old Pedant
12-28-2011, 08:23 AM
the short and simple answer is there is no equiv js "one line" conversion of int to bool

Not true.



function fxn(int){
document.getElementById('box').style.display = (int != 0) ? 'block' : 'none' ;
}

code somewhere else...
onmouseover="fxn(1);" onmouseout="fxn(0)"

The C# code you demonstrated (specifically, the Convert.ToBoolean(obj) call, though the function you wrote wouldn't compile because the obj argument had no type declaration) was attempting to convert obj to boolean, where obj was assumed to be of unknown type (and it would only work because of all the overloadings of ToBoolean, of course).

That's a lot different than int to boolean. Which I emphasized by naming the variable int in the above code (since "int" has no special meaning in JavaScript code).

However, there is *ALSO* a generic conversion of OBJECT REFERENCE to boolean in JavaScript. Notice that I am careful to say object REFERENCE. Just as in C# (and Java, but unlike C and C++), a variable in JavaScript can never contain an object, per se. A variable can only be a reference to an object.

And the implicit conversion of a reference, of any kind, to boolean in JavaScript and C# and Java is the same: If the reference is null the boolean value is false. All non-null references are implicitly true. [If you see an exception in C# or Java code, it will be because the class of the given object has declared its own to-boolean method, assuming that's legal in C#, which I think it is.]

So:


function fxn(objectReference){
document.getElementById('box').style.display = (objectReference != null) ? 'block' : 'none' ;
}

code somewhere else...
... onmouseover="fxn(new Date());" onmouseout="fxn(null)" ...


Now, it is also true that JavaScript will *implicitly* test an objectReference for null if you use it in a situation where a boolean is called for, so the above can be coded as simply


function fxn(objectReference){
document.getElementById('box').style.display = objectReference ? 'block' : 'none' ;
}

code somewhere else...
... onmouseover="fxn(new Date());" onmouseout="fxn(null)" ...

Personally, I find that kind of coding distasteful, as it hides the real meaning of the code, so I would alwyas use (objectReference != null) but you certainly don't have to do that.

If you really wanted to do it, you could build a generic anything-to-boolean conversion function.


<script type="text/javascript">
function toBoolean(x)
{
switch ( typeof x )
{
case "number": return x != 0;
case "string": return true;
case "object": return x != null;
case "undefined": return false;
case "boolean" : return x;
case "function" : return true;
}
}

function testit( )
{
var values = [ 0, 1, 9999, 3.1415, null, new Date(), "bananas", "", [1,2,3], true, false, toBoolean ];

for ( var v = 0; v < values.length; ++v )
{
value = values[v];
document.write( 'toBoolean(' + value + ') is <b style="color: red;">' + toBoolean(value) + '</b><br/>' );
}
}

testit();
</script>

(If you wished, you could define a blank string "" to be false. Since typeof "function" is really just another kind of object reference, and since a function reference is never null, returning true for that type is sensible. As is returning false for an undefined value.)

But you certainly don't need that for your actual integer-to-boolean conversion.

alykins
12-28-2011, 01:34 PM
situation is resolved- it doesn't really matter anymore; only posting to addres my typo and your comment- no what i posted would not compile; i missed an important peice as i was typing- my function call in C# would be


private void convertBool(int obj)

I forgot the type being passed- I used obj not in reference to object but just as a variable (yes I know to reserve it for good practices for objects- this was just an example) anyways yes good point what I had would not have compiled I was missing the data type- even if i was using type object I would have had to have (object obj) no matter what it would have error-ed out



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum