Jump to content

Pass variable from one function to another


mahsaad
 Share

Recommended Posts

Hello all,

I've read all the related topics, didn't find one to my question, so here it is

Consider this scenario:

;main au3 file

Opt("GuiOnEventMode", 1)

$main=GUICreate("Test", 200, 200)

$ticket_gui_btn = GUICtrlCreateButton("Ticket GUI", 10, 10, 50, 30)

GUICtrlSetOnEvent(-1, "ticket_gui")

#include <ticket.au3>

GUISetState(@SW_SHOW)

While 1
Sleep(1000) ;Idle around
WEnd

func close_func()
;bla bla bla
endFunc

;another file: ticket.au3

Func ticket_gui()

$child_gui = GUICreate("Test", 200, 200)

GUISetOnEvent($GUI_EVENT_CLOSE, "close_func")

$ticket_input = GUICtrlCreateInput("", 10, 10, 140, 23)

$send = GUICtrlCreateButton("Send", 10, 40 , 90, 25)
GUICtrlSetOnEvent(-1, "verify_gui")

GUISetState (@SW_SHOW, $child_gui)

endFunc

Func verify_gui()

$ticket_value=GUICtrlRead($ticket_input, 1)

if ticket_value="" then
msgbox(0, "error", "Ticket is empty")
endif

endFunc

If i declare the $ticket_input as a global variables outside the 2 functions, the variable $ticket_input is visible in the verify_gui() function and it works fine of course

My question: Is there an alternative instead of declaring it as a global variable?

PS: i have to many child windows and variables. And functions (gui and the verification ones) must be separate as the example above

Thanks guys

Update

For the ones who are suggestion using global variables, i'm already using that, i'm looking for an alternative solution

Using a message loop won't work since i'm using

Opt("GuiOnEventMode", 1)

If using global variables is the solution so be it, but i would like to read your opinion

Edited by mahsaad
Link to comment
Share on other sites

Something like this?

$variable = "It's me mario!"
_ShowMyVariable($variable)

Func _ShowMyVariable($passed_variable)
MsgBox(0,"","Your passed variable is: " & $passed_variable)
EndFunc

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

Something like this?

$variable = "It's me mario!"
_ShowMyVariable($variable)

Func _ShowMyVariable($passed_variable)
MsgBox(0,"","Your passed variable is: " & $passed_variable)
EndFunc

Hi!

Thanks for the reply

Your example the $variable = "It's me mario!" is outside a function, in that way it can be passed as a parameter in the function

In my case the variable is in a function and i want to see it in a second function

Link to comment
Share on other sites

  • Moderators

mahsaad,

the variable is in a function and i want to see it in a second function

Then declare the variable in Global scope or code in MessageLoop mode and pass it as a parameter. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Thanks for the reply

Your example the $variable = "It's me mario!" is outside a function, in that way it can be passed as a parameter in the function

In my case the variable is in a function and i want to see it in a second function

Like this?:

_First_Function()

Func _First_Function()
$variable = "It's me mario!"
_Second_Function($variable)
EndFunc

Func _Second_Function($passed_variable)
MsgBox(0,"","Your passed variable is: " & $passed_variable)
EndFunc

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

mahsaad,

Then declare the variable in Global scope or code in MessageLoop mode and pass it as a parameter. :)

M23

Thanks for the reply

I'm using global variables already, i'm looking for an alternative solution

I'm using

Opt("GuiOnEventMode", 1)

can't use message loop with that

Edited by mahsaad
Link to comment
Share on other sites

Look at my previous example :)

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

  • Moderators

mahsaad,

You have only those 2 choices: Global or MessageLoop. Your choice as to which you wish to use. :)

Although martin did produce a UDF to enable parameter use in OnEvent if you want to try it. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Like this?:

_First_Function()

Func _First_Function()
$variable = "It's me mario!"
_Second_Function($variable)
EndFunc

Func _Second_Function($passed_variable)
MsgBox(0,"","Your passed variable is: " & $passed_variable)
EndFunc

Thanks again,

That won't work either, the second function must be triggered when the button in the first function is pressed

Thanks any way

Link to comment
Share on other sites

mahsaad,

You have only those 2 choices: Global or MessageLoop. Your choice as to which you wish to use. :)

Although martin did produce a UDF to enable parameter use in OnEvent if you want to try it. ;)

M23

Thanks for the reply

I know about the udf, thanks for the suggestion, and i think those are the only solutions

I already started with global variables so i guess i'll continue that way

Plus the variables will be used in more than one function, in case i change the variable name, it will be easier to change it in one place instead of changing the parameters in all functions/files

Edited by mahsaad
Link to comment
Share on other sites

  • Moderators

mahsaad,

i think those are the only solutions

So my telling you exactly that twice already in this thread does not really convince you? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...