Jump to content

Reuse function variables in other function without Global


diff
 Share

Recommended Posts

Hello,

 

feeling dumb that I can't resolve my issue, my point is to not use Global variables in bigger projects at all.

So I have 2 functions, the first is main function and the second searching for information from websites and attaches the extracted information to variables.

 

Func main()
    
    ...does some code...
    ..then later i have some code which need
    to use variables from web_extraction() function..
    
    example:
    
    StringStripWS($var1, 3)
    
    Of course I get here error that variable $var1 possibly not
    declared
    
EndFunc 


Func web_extraction()
    
    ..does some code...
    ...extracts information from websites..
    
$var1 = "extracted info 1"
$var2 = "extracted info 2"

etc etc.

EndFunc

Of course I can write at the beginning 

Global $var1, $var2 and this will work, but how I can use $var1, $var2 which kill itself after function ends in main function?

 

I have read a lot of information about $params in function and etc. tried everything and still receive that the variables are possibly not declared and just stay blank inside.

Any examples how correctly to write this?

Link to comment
Share on other sites

Hello! Try something like this:

Func web_extraction($sToDo)
; $sToDo: 'Extract'  = extracts information from websites..
; $sToDo: 'GetInfo'  = return information collected before
;
Local Static $var[362]  ; for example, 362 items
    
If $sTodo = 'Extract'    Then
    ..does some code...
    ...extracts information from websites.. and fills the array with info
    $var[0] = "extracted info 1"
    $var[0] = "extracted info 2"
    ... etc ...
    Return
EndIf

If $sTodo = 'GetInfo'    Then
    ...does some code if needed
    Return $var
EndIf

EndFunc

 

Link to comment
Share on other sites

Hello, Maybe this:

 

_Test()

Func _Test()
    _1()
    _2()
    _3()
EndFunc   ;==>_Test


Func _1()
    ConsoleWrite("_1 Set MyData To 'Hello World'" & @CRLF)
    _MyData("Hello World")
EndFunc   ;==>_1

Func _2()
    ConsoleWrite("_2 MyData Value: " & _MyData() & @CRLF)
;~  _MyData("")  ;Clear
    _MyData(_MyData() & " - AutoIt Rocks!!!") ;Append Data
EndFunc   ;==>_2

Func _3()
    ConsoleWrite("_3 MyData Value: " & _MyData() & @CRLF)
EndFunc   ;==>_2

Func _MyData($Data = Default)
    Local Static $MyData = ""
    If @NumParams = 0 Then Return $MyData
    $MyData = $Data
EndFunc   ;==>_MyData

 

 

Saludos

Link to comment
Share on other sites

@Danyfirex Interesting way of using static

_ClassTest()

Func _ClassTest()
    _1()
    _2()
    _3()
EndFunc   ;==>_Test

Func _1()
    ConsoleWrite("Creating new class myclass" & @CRLF)
    _MyClass("Hello World")
EndFunc   ;==>_1

Func _2()
    ConsoleWrite("_2 MyData Value: " & _MyClass("Property2") & @CRLF)
;~  _MyData("")  ;Clear
    _MyClass("Property2",_MyClass("Property2") & " - AutoIt Rocks!!!") ;Append Data
EndFunc   ;==>_2

Func _3()
    ConsoleWrite("_3 MyClass data: " & _MyClass() & @CRLF)
    _MyClass("ha")
EndFunc   ;==>_2

func ha()
    consolewrite("Hello AutoIt")
endfunc
Func _MyClass($propName = Default, $propData=Default)
    Local Static $Property1 = " a "
    Local Static $Property2 = " b "
    Local Static $Property3 = " c "
    Local Static $Property4 = " d "
    local static $method1 = ha()
    If @NumParams = 0 Then Return "{" & $property1 & ";" & $property2 & ";" & $property3 & ";" & $property4 & "}"
    if @NumParams = 1 then
        if isdeclared($propname) then
            return eval($propName)
        Else
            $property1=$propname
        EndIf
    EndIf

    If @NumParams = 2 then
        if isdeclared($propname) then
            assign($propname,$propdata)
        endif
    EndIf
EndFunc   ;==>_MyClass

 

Link to comment
Share on other sites

Looking back at the initial problem posted by the OP, I don’t think it takes much more than:

Func main()

Local $var1, $var2

web_extraction($var1, $var2)
    
StringStripWS($var1, 3)
    
EndFunc 


Func web_extraction(ByRef $var1, ByRef $var2)
    
$var1 = "extracted info 1"
$var2 = "extracted info 2"

EndFunc

 

Code hard, but don’t hard code...

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...