Jump to content
Sign in to follow this  
Yeik

[Solved] func variable error

Recommended Posts

Yeik

i am having problems with a variable saying it isn't declared but it is declared in the function.

==> Variable used without being declared.:

If $str1 = "1" Then

If ^ ERROR

i can't figure out why.

Func _Sendmsg($str1 = "")
    ;If Not IsDeclared($str1) Then
    If $str1 = "" Then
        $szData = InputBox("Data for server", @LF & @LF & "Enter data to transmit to the server:", "", "", "", "", 200, 500)
    Else
        $szData = $str1
    EndIf
    TCPSend($sockclient, $szData)
EndFunc   ;==>_Sendmsg
Edited by Yeik

func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites
bo8ster

try == instead of =. Equality instead of assignment.

Also put AutoItSetOption("MustDeclareVars", 1) at the top of your script.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites
Malkey

i am having problems with a variable saying it isn't declared but it is declared in the function.

==> Variable used without being declared.:

If $str1 = "1" Then

If ^ ERROR

i can't figure out why.

Func _Sendmsg($str1 = "")
    ;If Not IsDeclared($str1) Then
    If $str1 = "" Then
        $szData = InputBox("Data for server", @LF & @LF & "Enter data to transmit to the server:", "", "", "", "", 200, 500)
    Else
        $szData = $str1
    EndIf
    TCPSend($sockclient, $szData)
EndFunc   ;==>_Sendmsg
"If $str1 = "1" Then", the error line, is not in the script you posted.

$str1 is a locally declared variable within the function you posted.

$str1 needs to be declare outside of this function if you wish to use $str1 outside of this function.

If you are using SciTE text editor. After running the script, by pressing "F5", you can double click on the red line in the output window at the bottom of the SciTE editor.

This will put a yellow dot next to the line in the script that has the error.

Share this post


Link to post
Share on other sites
SmOke_N

"If $str1 = "1" Then", the error line, is not in the script you posted.

It's not :D ? It's the first parameter of the function he posted.

Edit:

@OP - I obviously get the $socketclient isn't declared (because it isn't) with what you showed, but I'm sure?? you have it as a global?? .

But I don't get an error on $str1.

Do you have a reproduction script that gives this error that someone can test?

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Malkey

"If $str1 = "1" Then", the error line, is not in the script you posted.

It's not :huh: ? It's the first parameter of the function he posted.

Edit:

@OP - I obviously get the $socketclient isn't declared (because it isn't) with what you showed, but I'm sure?? you have it as a global?? .

But I don't get an error on $str1.

Do you have a reproduction script that gives this error that someone can test?

SmOke_N

$str1 = "" - Does appear - The first parameter of the function posted.

$str1 = "1" - Does not appear anywhere in the function posted. The error must be elsewhere.

Malkey

Share this post


Link to post
Share on other sites
SmOke_N

SmOke_N

$str1 = "" - Does appear - The first parameter of the function posted.

$str1 = "1" - Does not appear anywhere in the function posted. The error must be elsewhere.

Malkey

Ahh, nice catch.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
PsaltyDS

Aditionally, even if $str1 was declared globally, that function will still have a separate Local $str1 because it is declared as the input parameter to the function. So $str1 inside the function would be different than the Global $str1, which may or may not be desired behavior.

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Yeik

Ahh, nice catch.

Thats due to me trying to fix it by assigning a different value, i removed the one because thats what i orginally had but forgot that the last message i got was with it as a 1. I will try with ==, but i thought that == didn't work in autoit.

I will try some of the suggestions, if you want i can post the whole code.

Func _Sendmsg($str1 == "")
Edited by Yeik

func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites
Yeik

How is this Func Called?

maybe : Opt("OnExitFunc","_Sendmsg") ; ?

i edited my last message saying that i used trayitemsetonevent()

I also used AutoItSetOption("MustDeclareVars", 1) and had to fix several things in a few scripts, one being an include.

I can post my full code, it is similar to what i have posted about the display freezing but further along and i have changed it so i dont try and display an array in the function that is causing it to freeze.


func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites
Jos

I guess you didn't run your script through au3check (or are using the Full SciTE4AutoIt3 installation) ?

It would have given you a warning.

Try this script: Run it and it will display the MSGBOX. Then Right mouse click on the trayicon and select "Showinfo" to get the error.

Event functions do not initialize the parameter variables when called through an Event.

Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
; This works
ShowInfo("testje")
$infoitem = TrayCreateItem("Info")
TrayItemSetOnEvent(-1,"ShowInfo")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,"ExitScript")
TraySetState()
While 1
    Sleep(10)  ; Idle loop
WEnd

; Functions
Func ShowInfo($test="")
    Msgbox(0,"Info","Tray OnEvent Demo:" & $test)
EndFunc

Func ExitScript()
    Exit
EndFunc

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Yeik

I guess you didn't run your script through au3check (or are using the Full SciTE4AutoIt3 installation) ?

It would have given you a warning.

Try this script: Run it and it will display the MSGBOX. Then Right mouse click on the trayicon and select "Showinfo" to get the error.

Event functions do not initialize the parameter variables when called through an Event.

Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
; This works
ShowInfo("testje")
$infoitem = TrayCreateItem("Info")
TrayItemSetOnEvent(-1,"ShowInfo")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,"ExitScript")
TraySetState()
While 1
    Sleep(10) ; Idle loop
WEnd

; Functions
Func ShowInfo($test="")
    Msgbox(0,"Info","Tray OnEvent Demo:" & $test)
EndFunc

Func ExitScript()
    Exit
EndFunc

Jos

interesting.

So is there a fix for this?

Or do i just create two different functions for it?


func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites
Jos

interesting.

So is there a fix for this?

Or do i just create two different functions for it?

There are several workarounds for this depending on what you need the Func for.

Give a simple scriptlet that demonstrates why you need to call the Func with an OnEvent function and a regular call?

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Yeik

There are several workarounds for this depending on what you need the Func for.

Give a simple scriptlet that demonstrates why you need to call the Func with an OnEvent function and a regular call?

Jos

i was going to use it as a simple way to send messages through tcp that is already set up. so i can send exit messages, or return messages/completed messages.

this script i will be using as a client/server. It will be set up so that the server sends commands to the client, but if i want to add something i didn't want to create a whole new function for a custom message that will rarely be used.

It is actually going to be used to do remote installation, the msi's that i have require an interactive logon to do a full install. so it is executing the client, then the server will send the list of msi's to the client to run, after each it will send a message saying it is done with that msi. when it is all done there will be a message sent to the client that it is done and to exit.

Im making it as dynamic as i can so i can use the client/server code for anything that I need or may want to create in the future.


func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites
Jos

i was going to use it as a simple way to send messages through tcp that is already set up. so i can send exit messages, or return messages/completed messages.

this script i will be using as a client/server. It will be set up so that the server sends commands to the client, but if i want to add something i didn't want to create a whole new function for a custom message that will rarely be used.

It is actually going to be used to do remote installation, the msi's that i have require an interactive logon to do a full install. so it is executing the client, then the server will send the list of msi's to the client to run, after each it will send a message saying it is done with that msi. when it is all done there will be a message sent to the client that it is done and to exit.

Im making it as dynamic as i can so i can use the client/server code for anything that I need or may want to create in the future.

mmm, its still not clearto me why you need the parameter in the target Func when you use it through an Event.

Anyways, Just use a separate Func for the Event and have that call the Func that sends the message.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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  

×