Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pls help me with this if statement

Status
Not open for further replies.

pbanacos

IS-IT--Management
Apr 21, 2006
34
US
Hello,

I want to display all the printers connected to my computer system except the 'fax' and 'Microsoft Document Image Writer'. Does anyone have any suggestions? I believe my syntax in the IF statement is the problem. This is probably something very simple

I would appreciate any help...

Thanks

var WshNetwork = WScript.CreateObject("WScript.Network");
var WshShell = WScript.CreateObject ( "WScript.Shell" );

//New File System Object Creadted:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\LogFile.txt", true);
var Printers = WshNetwork.EnumPrinterConnections();
var i = Printers;

WScript.Echo("Results have been stored in logfile.txt");

for(i = 0; i < Printers.length; i += 2) {
if ( Printers.Item(i) == "Microsoft Office Document Image Writer" ||
Printers.Item(i) == "Fax") {
WScript.Echo("");
}
a.WriteLine("2Port " +Printers.Item(i)+ " = " + Printers.Item(i + 1));
}
a.Close();
 
Javascript uses square brackets instead of parentheses for indexing. Try
Code:
printers.item[i]

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
[tt]
for(i = 0; i < Printers.length; i += 2) {
if ( Printers.Item(i) == "Microsoft Office Document Image Writer" || Printers.Item(i) == "Fax") {
WScript.Echo(""); [green]//what for? simply comment it out[/green]
} [red]else {[/red]
a.WriteLine("2Port " +Printers.Item(i)+ " = " + Printers.Item(i + 1));
[red]}[/red]
}
[/tt]
ps. If you use .item (or .Item cases insensitive here), use parentheses, else a runtime error. If you prefer [] as if an array, use
[tt]
a.WriteLine("2Port " +Printers+ " = " + Printers[i + 1]);
[/tt]
 
Any reference as to WHY you access "Printers" that way?

I know from what little documentation I can find on MSDN that "Printers" is a collection (thus the use of the []), but normally "Item" is a property. In this case it looks more like a method. Would it be equally correct to say "Printers.Item"?

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
[1] Access via Printers.item(i) is well documented.
[2] Syntax item is a runtime error.
[3] Printers provokes no runtime error but it is returning undefined, so it is useless---my bad here.

Hence, the correct way is still Printers.item(i), more like the enumeration Printers is acting like a dictionary object indexed by zero-based integers.
 
Access via Printers.item(i) is well documented

WHERE is it well documented? I can find only one small example on MSDN, and it doesn't explain WHY it is done that way.

I'm not arguing with the way you are doing it, just trying to understand why it is being done in a way that looks peculiar and un-javascript-like to me.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
You can check it here.
Also browse to dictionary object for cross-examination, and that's why I said it is more like a dictionary. In any case, it is not rule written on stone to use index with square bracket. It is rather a habit too used to dealing with array. Just my opinion.
 
it is not rule written on stone to use index with square bracket
Yes, according to the javascript standard, it is. If you use square brackets, it is an index. If you use parentheses, javascript interprets it as a function call. Try swapping the use of them in pure javascript and see what happens.

Having said that, however, I have also figured out why you have to use the seemingly odd syntax above:

Javascript isn't a proper object-oriented language. While you can create your own objects in javascript, it lacks key elements of a true object-oriented language, such as the ability to set access privileges on object properties (i.e. public, private, friend), and the ability to define true "get" and "set" access methods for object properties, so you create true "read only" properties, nor can you validate values being assigned to properties, nor process property values before returning them.

It is possible to define indexed properties of an object, and access them with the square bracket notation, but all of the disadvantages mention in the paragraph above make it inadvisable in most circumstances.

The workaround to these shortcomings is to define your own access methods (such as "item" above). But because they really ARE methods, you have to use parentheses with them.

That's why, at first reading, the reference you cited above didn't make sense to me. "item" isn't really an indexed property at all, although you can act like it is. It is really an access method, and as such requires parentheses.

Similary for the dictionary object, which isn't an intrinsic javascript object, but an MS-created one.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
>Yes, according to the javascript standard, it is. If you use square brackets, it is an index. If you use parentheses, javascript interprets it as a function call.
What can I see, you are so indoctrinated and prefer words than substance.
 
What do you mean "indoctrinated"? I actually TRIED using both square brackets and parentheses to access an array. It works fine with square brackets, even when the "indices" are strings. When I used parentheses I get a function call error. The same thing happens if you try to access one of the DOM collections using parentheses instead of square brackets. That's about as substantial as you can get.

Did you actually read the rest of my post, or did you stop at the first thing you saw you disagreed with? I explained, in great detail, why you must use parentheses in the case mentioned above. It only appears to be accessing an indexed collection. It is actually a function call.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
If I or someone use wshnetwork, I don't know what else it be other than ms-created and maybe it is all their fault again---or, maybe not, it is mine?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top