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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with logical operator... 1

Status
Not open for further replies.

PScottC

MIS
Mar 16, 2003
1,285
US
Maybe I'm just broken, but I don't understand why the "or" operator won't work for me...

Doesn't work..
Code:
dim arrServers(10), intReturn
[blue]... populate array ...[/blue]
intReturn = InputBox("Type Server Number")
[red]If intReturn < LBound(arrServers) + 1 Or intReturn > UBound(arrServers) + 1 Then[/red]
MsgBox("You did not type the correct number")
End If

Does work...
Code:
[red]If intReturn => LBound(arrServers) + 1 And intReturn =< UBound(arrServers) + 1 Then[/red]

If I put a valid number between 1 and 11 in with the first set of code it doesn't work. If I put in a valid number in the second piece of code it works correctly... WHY?

BTW... If I replace "LBound(arrServers)" and "UBound(arrServers)" with actual integers, the Or operator evaluates correctly.


PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Main reason is probably str comparison. Add this before proceed to if-then.
[tt]
on error resume next
do while true
intReturn = InputBox("Type Server Number")
intReturn=clng(intReturn)
if err.number<>0 then
msgbox "Not a number as expected."
err.clear
else
If intReturn < LBound(arrServers) + 1 Or intReturn > UBound(arrServers) + 1 Then
MsgBox("You did not type the correct number")
else
exit do
End If
end if
loop
on error goto 0
[/tt]

 
Funny thing is that I already have something like that...

If Not IsNumeric(intReturn) Then
MsgBox("Not a number")
elseif [my code above]...
End If

Do I still have to convert the string to long?


PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
You do, else it is string comparison.
 
Actually I just did some more testing and I find that neither the "And" nor the "Or" operators evaluate my expression correcly.

If I'm using the "or" operator it always evaluates as "false-false". If I'm using the "and" operator it always evaluates "true-true". This is regardless of what values I put into the function or whether I "CLng" them.

I'm stumped.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Look, I do not want to drag along or divert attention. clng() or cint() will do. Precedence rule is [1] arithemtic then [2] comparison and finally [3] logical. The reason for the abberrations is as presented.
 
You are right... I was just looking at it upside down when I tried to reverse the operators. I should't be writing code this late at night.

intReturn = CLng(intReturn) is the correct fix for this.

Thanks for your help and patience. Have a star.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
And what about this ?
If (intReturn < LBound(arrServers) + 1) Or (intReturn > UBound(arrServers) + 1) Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
It improves readability hut it won't change the result. The comparison is the problem and the engine won't auto-coerce the intReturn to number (except say you do the hack intReturn*1) because string is acceptable for comparison operators as well as number.
 
PHV -- I did something close to that...
If intReturn < (LBound(arrServers) + 1) Or intReturn > (UBound(arrServers) + 1)

tsuji -- Understood. Thanks again.

Here is loop I ended up using..

Code:
Do
	If IsEmpty(intReturn) Then
		MsgBox("Reboot Cancelled")
		WScript.Quit
	Elseif Not IsNumeric(intReturn) Then
		Do
			intReturn = InputBox("You must type a number." & VbCrLf & _
				"Type the number of the Server you want to" & _
				" reboot" & VbCrLf & strMessage, "Which Server?")
		Loop Until IsNumeric(intReturn) = True
	End If
	intReturn = CLng(intReturn)
	if intReturn < (LBound(arrServers) + 1) Or intReturn > _
		(UBound(arrServers) + 1) Then
		intReturn = InputBox("You must type a number between " & _
			"1 and " & UBound(arrServers) + 1 & "." _
			& VbCrLf & strMessage, "Which Server?")
	Else
		ctr = 1
	End If
Loop Until ctr = 1



PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
You do not show the top of your code? Are you using DefInt a-z up there in the definitions? If not your intReturn will be a Variant (hence you can and are using IsEmpty on it) and InputBox will pass it a string value. In such case you could use;

If val(intReturn) < (LBound(arrServers) + 1) Or val(intReturn) > (UBound(arrServers) + 1)

Where intReturn is really a Variant (or String)

Personally I like to keep all the variable typing distint so would prefer;

dim a$, RetValue%
a$ = Inputbox(.....)
'do string based verification etc. on the string a$
'all being well convert it into a number
RetValue% = val(a$)
'do the numeric based verifications on the integer RetValue%

regards Hugh





 
Sorry this is the vbscript forum of course and you only have variants.

Hate that! Sorry again.
 
Which is of course my problem... I'm still a little green at VBScript (and programming in general) and I tend to forget about data types. I did have the realization that I needed to make sure the user of the script didn't put in text, but didn't realize that I had to make sure the data wasn't a Variant string when performing a mathematical operation on it.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top