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!

Advanced IF statements

Status
Not open for further replies.

josephk73

Technical User
Aug 1, 2002
115
GB
Hi guys my query is with the use of IF statements.

The following is a straight forward code snippet using an IF statement

Code:
string1 ="Zeus"

namer = Inputbox("Please enter a name")

If namer = string1 
 wscript.echo "name found"
    else
      wscript.echo "name not found"
End if
I've seen a code snippet which used (-1) to evaluate an expression. Hence the above code would be something like the following:

Code:
If namer = -1 Then
 wscript.echo "name found"
    else
      wscript.echo "name not found"
End if
However when I try to use this method, I dont get the correct evaluation even if I put the correct name in the Input box.

Can anybody help with this?

Cheers
 
What you probably saw was something more like:

If InStr(namer, string1) = -1 then




[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
If that isn't the case then please feel free to post a link to what you saw.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
If (namer = string1) = -1 Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi guys,

Thanks for getting back so quick.

This is snippet of what I saw:


Code:
strBMP= StrComputer(strSourceVersion, strBuild)

If strBMP  = -1 Then
objshell.Popup "Not correct"
End If

Thanks
 
There is a good argument for Boolean primitives. :)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
PHV, many thanks, but something is still missing :-(

Putting in your suggestion gives

Code:
string1 ="zeus"
namer = Inputbox("Please enter a name")

If (namer=string1)=-1 Then 
 wscript.echo "names not found"
    else
      wscript.echo "names found"
End if

If the correct string is entered in the input box, it always evaluates as "name not found". Conversely if the incorrect name is entered it evaluates as "names found"

Sorry to be a pain. But any more help please??!!

Cheers
 
What is the code for the StrComputer() function?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
You've inverted the test.

FYI:
-1 = True
0 = False



Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi EBGreen & PHV

The code snippet from the script is as follows:

Code:
StrComputer="."

 strBaseBuild  = "2.1.00"
strBSV = 2
regBuildSourceVersion ="HKLM\SOFTWARE\Source Version"
strSourceVersion =objShell.RegRead(regBuildSourceVersion)

'=================================================================================================================================
' Enumerate version
'=================================================================================================================================
        strBSV = StrComputer(strSourceVersion, strBuild)
    

      If strBSV  = -1 Then
           objShell.Popup "TERMINATING"
            WScript.Sleep 6000
           WScript.Quit(1)
      End If

Again Guys, cheers.
 
So the code you posted doesn't make much sense. First, StrComputer is used as a variable then it is used as a function.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Sorry guys, correction in the code I just posted.

strBaseBuild should read strBuild

Also strComp is defined as a constant at the beginning of the script (ie Const strComp=".")


Sorry for any confusion.

Cheers
 
Guys,

I think I get the script based on understanding the significance of -1.

strBSV is given a random value at the beginning of the script (in this case 2)

However later strBSV is assigned the value of StrComputer(strSourceVersion, strBuild).

If the registry values have not be written correctly then the line strBSV = StrComputer(strSourceVersion, strBuild) will evaluate incorrectly, which would then mean the earlier value of strBSV would still be 2.

If this is the case, the when strBSV is tested with the IF statement, the random value of 2 is returned (which is incorrect) and the script terminates.

Does this make any sense?
 
> Does this make any sense?

No.

StrComp is a reserved word. It is VBScript function (see here).

The line of code Const strComp="." will return an error. So, you either have an "On Error Resume Next" before the above line of code is called (suppressing errors... which you should not be doing if you are troubleshooting), or you have again not posted your actual code.

My suggestion is to post your "StrComputer" function, and the code that calls it (with a copy/paste!)
 
Dudes, it all makes sense. Thanks to guitarzan and everybody that contributed.

This is not my script, and I have used strComputer as a variable in the past. Very stupidly, I assumed strComp was just another variable for the local hostname.

Since this is not the case, the whole thing makes perfect sense.

The script is comparing two variables and depending on the result of the comparison (ie if one string is less than the other (-1))then the code quits. If the comparison results in an equal or greater than, the code continues.

Again, thanks to everybody and I hope I haven't wasted to much of your time. If anything, I've learnt something tonight.

Good night and cheers again.


 
Ahh that actually makes sense now, glad you got it sorted out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top