Sign in to follow this  
Followers 0
Sundance

Function Parameter Bug ?

5 posts in this topic

Hiho,

i don't know if this behavior is normal but i would like to hear your thoughts.

Look at the following code.

_Test(0)

Func _Test($iFirst = "nothing")
If $iFirst = "nothing" Then
  ConsoleWrite("$iFirst = nothing" & @CRLF)
  ConsoleWrite("Parameterval: " & $iFirst & @CRLF)
EndIf
EndFunc

The if expression inside the function will be true even when i do give the function the paramter 0.

Is this a bug. In my special case i have an optional paramter which can be range from 0 to n. But at the moment i can't pass the value 0 to my function...

Hope someone can clarify this for me and thanks in advance

Sundance

Share this post


Link to post
Share on other sites



_Test(0)

Func _Test($iFirst = "nothing")
    If $iFirst == "nothing" Then
        ConsoleWrite("$iFirst = nothing" & @CRLF)
        ConsoleWrite("Parameterval: " & $iFirst & @CRLF)
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

Sundance,

You are mixing string and number values in your If statement which will usually end in tears. "nothing" will be converted to a number value of 0, which coincidentally is the value you compare it to. LaCastiglione's suggestion to use the == operator will force a string comparison and thus work correctly. :)

As a general rule avoid number/string comparisons as they can give you results you do not expect - even though AutoIt is handling them as best it can it cannot read your mind. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

In this case, I would use either the keyword Default and/or @NumParams

ConsoleWrite(">_Test()" & @LF)
_Test()
ConsoleWrite(">_Test(Default)" & @LF)
_Test(Default)
ConsoleWrite(">_Test(0)" & @LF)
_Test(0)

Func _Test($iFirst = Default)
    If @NumParams = 0 Then
        ConsoleWrite("No parameter" & @LF)
    ElseIf $iFirst = Default Then
        ConsoleWrite("Parameter Default" & @LF)
    EndIf
EndFunc

ConsoleWrite(">_Test2()" & @LF)
_Test2()
ConsoleWrite(">_Test2(Default)" & @LF)
_Test2(Default)
ConsoleWrite(">_Test2(0)" & @LF)
_Test2(0)

Func _Test2($iFirst = Default)
    If $iFirst = Default Then
        ConsoleWrite("Parameter Default" & @LF)
    EndIf
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Oh my god. I knew it was my fault. How could i mix an integer with a string. Yesterday i was very sleepy and in the middle of programming i felt asleep! ;)

Sorry for posting such a dump question and many thanks for your answers!! I didn't knew @NumParams. Also a possible way.

Thx again folks!! Today i fell better and got enough sleep last night so and think a won't post today :)

greetz

Sundance

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  
Followers 0