Jump to content

Defining Variables


Ghost21
 Share

Recommended Posts

When I call a function and pass a Variable to it like for example $pc and then inside that function together with $pc I get a new variable that I define like $memory. When I leave the function I loose the Newley defined $memory Variable. Is there a way to retain that to use it somewhere else in my code.. ???

Link to comment
Share on other sites

You can do this a few ways:

If you only need to modify one variable:

$memory = doStuff()
 Func doStuff($pc)
     $memory = 512
    Return $memory
 EndFuncoÝ÷ ٲʧyçm¢jü²f¥©¨º{h~ö«¦åzÏÛjëh×6Dim $memory

doStuff()
Func doStuff($pc, ByRef $memory)
    $memory = 512
EndFuncoÝ÷ ٲʧyçm¢jüh~ö«¦åzÏÛjëh×6doStuff()
  Func doStuff($pc)
      Global $memory = 512
  EndFuncoÝ÷ ÚÀ+Ú®&ç^rV«yÚ.¶Èy©ò~éܶ*'²+¢{"uêÞv hm©^½éðØhºÛaxihm©]yÉZ­«b¢v®¶­sdFÒb33c¶ÖVÖ÷'¦Fõ7GVfb¤gVæ2Fõ7GVfbb33c·2¢b33c¶ÖVÖ÷'ÒS ¤VæDgVæ0
Edited by weaponx
Link to comment
Share on other sites

Wow, Thanks WeaponX

I tried still not working something I'm missing ???

Dim $adobe

adobe()

Func adobe($pc, ByRef $adobe)

$Adobe = FileGetVersion("\\" & $PC & "\c$\" & "Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe")

msgbox(4000,"Test Adobe Version",$adobe) <--- This Works

EndFunc

msgbox(4000,"Test Adobe Version",$adobe) <--- Gone again

But after that goto another function

And I call all the Variables back think I have like 60 of them : /

func sqladdtolist(pc,memory,etc,adobe,etc)

msgbox(4000,"Test Adobe Version",$adobe) <--- Still nothing

Link to comment
Share on other sites

Wow, Thanks WeaponX

I tried still not working something I'm missing ???

Dim $adobe

adobe()

Func adobe($pc, ByRef $adobe)

$Adobe = FileGetVersion("\\" & $PC & "\c$\" & "Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe")

msgbox(4000,"Test Adobe Version",$adobe) <--- This Works

EndFunc

msgbox(4000,"Test Adobe Version",$adobe) <--- Gone again

But after that goto another function

And I call all the Variables back think I have like 60 of them : /

func sqladdtolist(pc,memory,etc,adobe,etc)

msgbox(4000,"Test Adobe Version",$adobe) <--- Still nothing

If you run a script with full verification turned on, you get warnings about DIM being depricated - explicitly using GLOBAL or LOCAL is preferred.

Your call to adobe() is invalid because you don't provide valid parameters.

The variables $pc and $adobe are LOCAL to the function because they are declared in the Func line as parameters. You also happen to have a global variable with the same name, but they are not the same variable (well, they are because of the ByRef, but still would be with different names due to that).

Try it this way to illustrate the proper usage:

Global $adobe

adobe(@ComputerName, $adobe)
MsgBox(4000, "Results", "Test Adobe Version = " & $adobe)

Func adobe($pc, ByRef $sVer)
    $sVer = FileGetVersion("\\" & $pc & "\c$\" & "Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe")
    MsgBox(4000, "Debug", "Debug inside adobe() - $sVer = " & $sVer)
EndFunc   ;==>adobe

:P

Edited by PsaltyDS
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
Link to comment
Share on other sites

This works for me:

Dim $memory

doStuff($memory)
MsgBox(0,"", $memory)

Func doStuff(ByRef $m)
    $m = 512
EndFuncoÝ÷ Øíê®j{*½ªâi¹^²­²éæjv i¹^ʦk,Ûh${h¶r¸©¶)à'-Ú¯*ç-jëh×6; Create dictionary object
Dim $oDictionary = ObjCreate("Scripting.Dictionary")

doStuff()

MsgBox(0,"",$oDictionary.Item("cpu") & @CRLF & $oDictionary.Item("hd") & @CRLF & $oDictionary.Item("memory"))

Func doStuff()
    $oDictionary.Add("cpu", 2400)
    $oDictionary.Add("hd", 80)
    $oDictionary.Add("memory", 1024)
EndFunc
Link to comment
Share on other sites

Yeah the

Create dictionary object

Dim $oDictionary = ObjCreate("Scripting.Dictionary")

doStuff()

MsgBox(0,"",$oDictionary.Item("cpu") & @CRLF & $oDictionary.Item("hd") & @CRLF & $oDictionary.Item("memory"))

Func doStuff()

$oDictionary.Add("cpu", 2400)

$oDictionary.Add("hd", 80)

$oDictionary.Add("memory", 1024)

EndFunc

I could see being very usefull ... Now I'm up to almost 74 Variables .. Is there some place that I can get more info on that ????

Is there any speed loss ??

what I found that worked is if I did:

$Adobe = FileGetVersion("\\" & $PC & "\c$\" & "Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe")

MsgBox(4000, "Adobe Acrobat", $Adobe)

Global $dummyvariable = $Adobe

Link to comment
Share on other sites

I'm sure it's a little slower than using just plain variables or an array, but it's the next best thing to an associative array (which autoit doesn't support).

There are more things you can do with the Scripting Dictionary too. See here: http://msdn2.microsoft.com/en-us/library/x4k5wbx4.aspx

Global $computerArray[1]
$computerArray[0] = 0

;Add new PC with 2400mhz cpu, 80GB harddrive, 1 GB ram
addPC(2400, 80, 1024)

;Add new PC with 3600mhz cpu, 320GB harddrive, 2 GB ram
addPC(3600, 320, 2048)

$string = ""
;Loop through all computer objects
For $X = 1 to $computerArray[0]
    
    ;Verify the current element is an object
    If IsObj($computerArray[$X]) Then
        $string &= "Computer " & $X & ":" & @CRLF
        
        ;Loop through all keys in object
        For $key In $computerArray[$X].Keys
            $string &= $key & ": " & $computerArray[$X].Item($key) & @CRLF
        Next
        $string &= "-------------------" & @CRLF
    EndIf      
Next

MsgBox(0,"", $string)


Func addPC($cpu, $harddrive, $memory)
    Local $oDictionary = ObjCreate("Scripting.Dictionary")
    
    $oDictionary.Item("cpu") = $cpu
    $oDictionary.Item("hd") = $harddrive
    $oDictionary.Item("memory") = $memory
    
    ;Retrieve current number of elements in array
    $numElements = Ubound($computerArray)
    
    ;Resize array to accomodate new object
    Redim $computerArray[$numElements + 1]
    
    ;Store object in array
    $computerArray[$numElements] = $oDictionary
    
    ;Increment counter
    $computerArray[0] += 1
EndFunc
Edited by weaponx
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...