Inpho

If statement misbehaving severely

4 posts in this topic

Hi all, again

So, this code, I got no idea what's going on. The function _CPUploadFiles returns a file. The first msgbox in the ActionHandler shows that the variable contains the correct filename, but it will always end up in the wrong part of the if statement. Am I going mad?

iFqbIlM.png

Func _CPActionHandler($_iAHArrayRow, $_iAHArrayCol)
    Local $_sAHLocalFile, $_sAHRemoteFile
    If $_iAHArrayCol = 0 Then
        Switch $_iAHArrayRow
            Case 1
                $_sAHLocalFile = _CPUploadFiles()
                MsgBox(0, "", $_sAHLocalFile)
                ClipPut($_sAHLocalFile)
                If $_sAHLocalFile = 0 Then
                    MsgBox(0, "wat", $_sAHLocalFile)
                    Return (0)
                Else
                    $_sAHRemoteFile = $_sAHLocalFile
                    MsgBox(0, $_sAHLocalFile, $_sAHRemoteFile)
                    ProgressOn("Upload Progress", $_sAHLocalFile)
                    _FTP_ProgressUpload ( $hFTPConnectSession, $_sAHLocalFile, "/" & $_sAHRemoteFile, _UpdateProgress)

                    ProgressOff()

                    ;_FTP_ProgressUpload ( $hFTPConnectSession, $_sAHFile, "/Scripts/test2/" & $_sAHFile, _UpdateProgress)
                    $aa = @error
                    MsgBox(0, "", $aa)
                EndIf
        EndSwitch

    EndIf
EndFunc   ;==>_CPActionHandler

Func _CPUploadFiles()
    Return(FileOpenDialog("Select File", @ScriptDir, "All (*.*)"))
EndFunc   ;==>_CPUploadFiles

 

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

when you check if a string is not empty, compare it against an empty string, not against zero:

replace this line:

If $_sAHLocalFile = 0 Then

with this:

If $_sAHLocalFile = '' Then

 

Edited by orbs

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Many thanks guys, I can't believe I haven't run into this problem before. It now reminds me of an example script I saw some time ago where a function would return * instead of 0. Makes sense, I'm checking for an integer when the return is a string. I figured returning 0 from a failed function would be fine, but I se the potential problems now. Looks like it's time for me to look into SetError.

Func _CPActionHandler($_iAHArrayRow, $_iAHArrayCol)
    Local $_sAHLocalFile, $_sAHRemoteFile
    If $_iAHArrayCol = 0 Then
        Switch $_iAHArrayRow
            Case 1
                $_sAHLocalFile = _CPUploadFiles()
                MsgBox(0, "", $_sAHLocalFile)
                ClipPut($_sAHLocalFile)
                If $_sAHLocalFile = "*" Then
                    MsgBox(0, "", $_sAHLocalFile)
                    Return (0)
                Else
                    $_sAHRemoteFile = $_sAHLocalFile
                    MsgBox(0, $_sAHLocalFile, $_sAHRemoteFile)
                    ProgressOn("Upload Progress", $_sAHLocalFile)
                    _FTP_ProgressUpload ( $hFTPConnectSession, $_sAHLocalFile, "/" & $_sAHRemoteFile, _UpdateProgress)

                    ProgressOff()

                    ;_FTP_ProgressUpload ( $hFTPConnectSession, $_sAHFile, "/Scripts/test2/" & $_sAHFile, _UpdateProgress)
                    $aa = @error
                    MsgBox(0, "", $aa)
                EndIf
        EndSwitch

    EndIf
EndFunc   ;==>_CPActionHandler

Func _CPUploadFiles()
    $_sUFFile = FileOpenDialog("Select File", @ScriptDir, "All (*.*)")
    If @error Then Return("*")
    Return($_sUFFile)
EndFunc   ;==>_CPUploadFiles

 

Edited by Inpho

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