Jump to content
Sign in to follow this  
tsolrm

This must be a joke. If/EndIf

Recommended Posts

tsolrm

This has been driving me insane for the last 2 hours.

I'm not gonna post the whole code, just the relevant chunks. This is the loop.

So the problem is, the code completely ignores "If $test = 0 Then" and carries on doing things that should not be done unless $test = 0.

$test is returned by a function but this function never returns zero, because the message box i've set up never shows up.

Please help(

While 1
$Accept = TCPAccept ($TCPListen) ; Keep accepting connections
If $Accept <> -1 Then
  _ArrayAdd ($Socket, $Accept) ; If a connection is made - it's added to a list.
  $Max = $Max + 1 ;Number of connections
EndIf
For $i=1 to $Max ;Keep recieving data from all the connections
  $Message = TCPRecv ($Socket [$i], 100000)
  Sleep (10)
  If @error Then TCPClose ($i);If there is an error - close the connection
  If $Message <> "" Then ; If the message recieved is not empty
   $test = LogIn ($Message) ;$test is the return value
   MsgBox (0, "test", $test) ;Here's the kicker, this messagebox shows a value that is NOT zero.
   If $test = 0 Then
    MsgBox (0, "test", $test) ;Even though for some reason the value above was not zero it goes to this stage and the messagebox displays THE SAME value,
    SendAll ($Message)
    MsgBox (0, "test", $test) ;Again, same value for $test
   ElseIf $test = 1 Then
    TCPSend ($Socket [$i], "Not found")
    TCPCloseSocket ($Socket[$i])
    _ArrayDelete ($Socket, $i)
   Elseif $test <> 0 and $test <> 1 Then ;This is what should happen but it's just ignored!
    $SendBack = LogIn ($Message)
    TCPSend ($Socket [$i], $SendBack)
    $split = StringSplit ($SendBack, ",")
    _ArrayAdd ($Users, $split[1])
   EndIf
  EndIf
Next
WEnd

Func LogIn ($String)
If StringLeft ($String, 19) = "SomethingThatHas19Digits" Then
  $String = StringTrimLeft ($String, 19)
  Local $Ladder
  _FileReadToArray("Ladder.txt", $Ladder)
  $Search = _ArraySearch  ($Ladder, $String, 1, 0, 0, 1)
  If $Search <> -1 Then return $Ladder [$Search]
  If $Search = -1 Then return 1
Else
  return 0
  MsgBox (0, "test", "returned 0")
EndIf
EndFunc

Share this post


Link to post
Share on other sites
lark

The message box you have set to show up after returning zero will never show up because... the script had just returned zero.

return 0
  MsgBox (0, "test", "returned 0")

If the script returns zero, its not going to keep executing the function.

Share this post


Link to post
Share on other sites
tsolrm

The message box you have set to show up after returning zero will never show up because... the script had just returned zero.

return 0
  MsgBox (0, "test", "returned 0")

If the script returns zero, its not going to keep executing the function.

That wasn't the problem. It seems that i've done it now. Instead of using 1 and 0 I used "yes" and "no". That seems to have done the trick. No clue why though

Share this post


Link to post
Share on other sites
MilesAhead

I think what's messing you up is in different places in Login() you variously return strings or numbers. I think AutoIt looks at it and assumes the return type is String. It's probably returning the 0 or 1 as a "0" or "1".

Try using a ByRef param to return the data and a return type of True or False to indicate success. Or return the string but return "" on error and set the error code using SetError().

  • Like 1

Share this post


Link to post
Share on other sites
AdmiralAlkex

I'm tired and needs to get some sleep, so I'm just gonna quote the three biggest misses I see.

If a string is used as a number, an implicit call to Number() function is done. So if it doesn't contain a valid number, it will be assumed to equal 0. For example,

10 * "fgh" equals the number 0

Return 0
  MsgBox(0, "test", "returned 0")

Sleep(10)
  If @error Then TCPClose($i);If there is an error - close the connection

@MilesAhead

The return type is what it is, it doesn't change. The comparison is all.

Edited by AdmiralAlkex
  • Like 1

Share this post


Link to post
Share on other sites
Spiff59

Your Login() function is returning either a numeric variable (0 or 1), or a string variable ($Ladder[$Search]).

After the function you compare the returned value to both numeric 0, then numeric 1.

That works fine if you return 0 or 1, and compare a numeric to a numeric. but not when you return a string and are comparing a string variable to a numeric.

When comparing a string to a numeric using the "=" operator, the string is converted to a numeric value using the following rules:

1. Empty strings or strings beginning with a non-numeric character are converted to a numeric 0 ("" = 0, "dog" = 0)

2. Strings beginning with numeric characters are truncated at the first non-numeric character ("+123ABC" = 123).

The "==" operator behaves differently from "=" by not forcing a conversion of the string.

So "" is not equal to any numeric value, neither are " " or "dog" or "123ABC" equal to any numeric value.

The "==" operator does still consider a string to match a numeric if the string consists of ONLY numeric characters ( "-77.7" = -77.7)

Then there are boolean compares out there, where you'll see only one variable shown and no operator, like "If $value Then ...". Those work on their own rules, If $value is a numeric variable, then any non-zero value will return true and only 0 returns false. If $value is a string, then only an empty/null string ("") retuens false, and anything else true.

Compare("empty string", "", 0)
Compare("mixed string starting with alpha", "x1", 1)
Compare("mixed string starting with number", "+1x", 1)
Compare("string contains only numeric characters", "1.1", 1.1)

Func Compare($title, $a, $b)
    Local $comparestr = "$a: " & $a & "  (" & VarGetType($a) & ")" & @CRLF & "$b: " & $b & "  (" & VarGetType($b) & ")"
    Local $r1 = $a = $b
    Local $r2 = $a == $b
    MsgBox(0, $title, $comparestr & @CRLF & @CRLF & "Converted Result =  " & $r1 & @CRLF & "Unconverted Result == " & $r2)
EndFunc

There are descriptions of string conversion it in the helpfile, mainly in the "Language Reference" section.

Edit: I think you could also have fixed your original code by just changing test = 0 and test = 1 to test == 0 and test == 1

Edited by Spiff59
  • Like 1

Share this post


Link to post
Share on other sites
tsolrm

Thanks guys!

Share this post


Link to post
Share on other sites
MilesAhead

@AdmiralAlkex Right. Everything is just a Variant after all. In any case the numeric conversion is the culprit. Untyped return is both a strength and weakness. No error catching via returning the "wrong" type if there is such a thing.

I had a similar experience with values from .ini files. Someone pointed out they are always read as string. After a short period of playing around with functions like Number() I wrote a simpler function:

Func _IniBool($val)
    If $val = "True" Or $val = "1" Then Return True
    Return False
EndFunc   ;==>_IniBool

Convenient for wrapping an IniRead such as:

Local $useFeature = _IniBool(IniRead($iniFile, "Settings", "TheFeature", False))
Edited by MilesAhead

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×