To the intellectual programmers: if (object) question (1 Viewer)

BlueIshDan

☠
Local time
Today, 03:13
Joined
May 15, 2014
Messages
1,122
Though I have some intellectual understanding of programming and the computer, I have more of an intuitive understanding of how everything works together.

I was wondering why, in many languages, this works:

Code:
if (object) {

}

NOW this works sometimes. I was wondering if anyone could explain to me when and why it does and doesn't work.

Feed my brain please! :)

FYI here is another, more in depth, example:

Code:
[B][COLOR="Red"][FONT="Arial Black"] DECLARATION [/FONT][/COLOR][/B]
var XMLHTTPRequestObject = false;

[B][COLOR="Red"][FONT="Arial Black"] INIT [/FONT][/COLOR][/B]
if (window.XMLHttpRequest) {
    XMLHTTPRequestObject = new XMLHttpRequest;
} else {
    XMLHTTPRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function getData(dataSource, divID) {

[B][COLOR="Red"][FONT="Arial Black"] QUESTION [/FONT][/COLOR][/B]
    if (XMLHttpRequestObject) {
        var obj = document.getElementById(divID);
        XMLHTTPRequestObject.open("GET", datasource);

        XMLHTTPRequestObject.onreadystatechange = function () {

            if (XMLHTTPRequestObject.readystate == 4 && XMLHTTPRequestObject.status == 200) {
                obj.innerHTML = XMLHTTPRequestObject.responseText;
            }
        }

        XMLHTTPRequestObject.send(null);

    }
}

Thanks!
Dan
 

ConnorGiles

Strange Traveller
Local time
Today, 06:13
Joined
Aug 12, 2014
Messages
1,068
I did Programming as a minor part to my IT Practitioners Diploma in college and to me -

If (Object) {


}

can be used in many scripting languages - to which are you referring?

I'm assuming HTML/Java but just need to check
 

BlueIshDan

☠
Local time
Today, 03:13
Joined
May 15, 2014
Messages
1,122
I've seen it all around since I've started. So over the past 11-12 years I didn't really keep track of which language I've seen it. LOL :p
 

BlueIshDan

☠
Local time
Today, 03:13
Joined
May 15, 2014
Messages
1,122
My instinct is telling me that it has to do with variants that you can initially... initialize to false then assign it to hold an object. But it also tells me that in this case there will be an object in it regardless of what the initial boolean value was. FRIIGGG
 

ConnorGiles

Strange Traveller
Local time
Today, 06:13
Joined
Aug 12, 2014
Messages
1,068
When I used it it was true and false statements also - this would also be used for if else statements.

If something is this then do this

if not "else" do this

or "else" do this

and so forth.
 

pr2-eugin

Super Moderator
Local time
Today, 06:13
Joined
Nov 30, 2011
Messages
8,494
I am not 100% sure, but... Since the variable is declared as Variant, it takes the first initialization to be the Datatype of the Variable and cannot be reassigned.

Again, to create an Object or to assign a variable to an object you might have to SET it over just assigning it by using "="
 

ConnorGiles

Strange Traveller
Local time
Today, 06:13
Joined
Aug 12, 2014
Messages
1,068
As Paul said Dan,

It is a variant - "If its not equal to this, do this or this or this etc..."

Which is displayed as :

Code:
If (object) {  
blahblah
} 

else {
blahblah
}


else {
blahblah
}
 

pr2-eugin

Super Moderator
Local time
Today, 06:13
Joined
Nov 30, 2011
Messages
8,494
Ya lost me hahaha
I think I understand what Connor is on about, in some languages you have to explicitly mention even thought logically speaking it is the opposite.

Example,
Code:
If 5 > 5 Then 
    Debug.Print "Not Executed"
ElseIf 5 = 5 Then
    Debug.Print "Executed"
Else 
    Debug.Print "Not Executed"
End If
As you can see 5>5 or 5<5 is both logically incorrect. So in this scenario we need to explicitly check for equivalency. I think this is what he says, or maybe I am just barking up the wrong tree. ;)
 

BlueIshDan

&#9760;
Local time
Today, 03:13
Joined
May 15, 2014
Messages
1,122
None of this really answers the logic behind what is happening here.

The var is initialized as a boolean = false.

then within the assigning of an AJAX handler:
Code:
if (window.XMLHttpRequest) {
    XMLHTTPRequestObject = new XMLHttpRequest;
} else {
    XMLHTTPRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

the variant is always assigned an object.

then in the condition statement it checks the object as if it was still a boolean.
 

ConnorGiles

Strange Traveller
Local time
Today, 06:13
Joined
Aug 12, 2014
Messages
1,068
I'm not quite sure your objective here Dan.

If statements unlike booleans can have more than 2 possible outcomes.

they can check if something is true if the first option wasn't and keep going through the else statements until one actually turns out to be true :)

(Sorry if I'm not making much sense, it does in my mind :p)

You can have many else statements as you like after the If statement.

Whereas with a boolean, its more of a "If its true, do this" - "If its false, do this"

this is what I was taught anyway :p.
 

BlueIshDan

&#9760;
Local time
Today, 03:13
Joined
May 15, 2014
Messages
1,122
I understand how a condition statement works.

I'm asking about how it takes an single object and determines a boolean:

if (condition:boolean outcome) {

}

What the question is, is how and in what scenarios does the condition accept an object and determine its validity as a boolean outcome which the if (condition statement) can determine.
 

BlueIshDan

&#9760;
Local time
Today, 03:13
Joined
May 15, 2014
Messages
1,122
My apologies for the first question being so vague and not really being a question at all! I see why you would think why I was asking how a condition statement works. haha

DURR
 

ConnorGiles

Strange Traveller
Local time
Today, 06:13
Joined
Aug 12, 2014
Messages
1,068
I'm going to take a stab in the dark here,

I'd say if the possible outcome has more than 2 options.
 

BlueIshDan

&#9760;
Local time
Today, 03:13
Joined
May 15, 2014
Messages
1,122
I feel like I really want to know how this works and where it works because it can be really easy & effective for validation programming :p
 

MarkK

bit cruncher
Local time
Yesterday, 23:13
Joined
Mar 17, 2004
Messages
8,178
Here, check out how javascript evalutes booleans http://www.w3schools.com/js/js_booleans.asp It does implicit conversions with much more latitude than VBA.

"Everything without a real value evaluates to false", so Nothing in VBA, Null in VBA, in javascript would evaluate to false.

So . . .
Code:
If (someObject)
. . . in javascript returns true if the object is not nothing, and is not null, and the specific code you posted . . .
Code:
if (window.XMLHttpRequest) {
    XMLHTTPRequestObject = new XMLHttpRequest;
} else {
    XMLHTTPRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
. . . tests the capability of the browser. If it returns a valid window.XMLHttpRequest, great--execute the first block, but some older versions of IE did not, in which case you'd need to create the active x object.
 

ConnorGiles

Strange Traveller
Local time
Today, 06:13
Joined
Aug 12, 2014
Messages
1,068
From what I was learnt :

Boolean - 2 options , This or This basically.

If statement - This, or This, or This etc....

Pretty sure thats how it works :D
 

BlueIshDan

&#9760;
Local time
Today, 03:13
Joined
May 15, 2014
Messages
1,122
Thank you very much Mark.
So it simply depends on how a language evaluates booleans.

Because I work in and jump around between so many languages. I find myself flowing into trying to use this is some of the wrong ones. Maybe that was my confusion thinking there was some kind of instance where it would work and not.

Great! Thanks :D
 

BlueIshDan

&#9760;
Local time
Today, 03:13
Joined
May 15, 2014
Messages
1,122
Though I have some intellectual understanding of programming and the computer, I have more of an intuitive understanding of how everything works together.

I was wondering why, in many languages, this works:

Code:
if (object) {

}

NOW this works sometimes. I was wondering if anyone could explain to me when and why it does and doesn't work.

Feed my brain please! :)

FYI here is another, more in depth, example:

Code:
[B][COLOR="Red"][FONT="Arial Black"] DECLARATION [/FONT][/COLOR][/B]
var XMLHTTPRequestObject = false;

[B][COLOR="Red"][FONT="Arial Black"] INIT [/FONT][/COLOR][/B]
if (window.XMLHttpRequest) {
    XMLHTTPRequestObject = new XMLHttpRequest;
} else {
    XMLHTTPRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function getData(dataSource, divID) {

[B][COLOR="Red"][FONT="Arial Black"] QUESTION [/FONT][/COLOR][/B]
    if (XMLHttpRequestObject) {
        var obj = document.getElementById(divID);
        XMLHTTPRequestObject.open("GET", datasource);

        XMLHTTPRequestObject.onreadystatechange = function () {

            if (XMLHTTPRequestObject.readystate == 4 && XMLHTTPRequestObject.status == 200) {
                obj.innerHTML = XMLHTTPRequestObject.responseText;
            }
        }

        XMLHTTPRequestObject.send(null);

    }
}

Thanks!
Dan

I found this code online a while back, and what bugs me is that my intuition says there is no need to initialize the variant to false. Is there?
 

Users who are viewing this thread

Top Bottom