Jump to content

Hiding variables outside a udf


Surya
 Share

Recommended Posts

Hi everyone ,I was wondering wether i could hide variables outside a UDF,like this

;This is the UDF named myudf.au3

Global $myvariable1 = "sample1", $myvariable2
;these variasbles are visible and accessed when the udf is included

Localize $myvariable3 = "sample2"
;i want this variable to be invisible to the mainscript that includes the UDF
;ie it should be visible here but not in the script that uses it
 Dummy()

Func Dummy()
    Msgbox(default,"variables",$myvariable1 &" " & $myvariable3)
    ;This should run without an error
EndFunc   ;==>Dummy
;including the previous UDF

#include "myudf.au3"

Msgbox(default,"first variable",$myvariable1 &" This was a variable defined in the UDF") ; this successfully shows a msgbox
Msgbox(default,"special variable",$myvariable3) ; this should show an error showing the variable $myvariable3 doesnt exist

Is there someway to hide variables in the mainscript while using them in the UDF^_^

Thankyou

Edited by Surya

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Link to comment
Share on other sites

If variable3 is local it works as you want

Func Dummy()
    Local $myvariable3 = "sample2"
    Msgbox(default,"variables",$myvariable1 &" " & $myvariable3)
    ;This should run without an error
EndFunc   ;==>Dummy

The idea is that Local scope inside functions means that variables are destroyed at the end of the function, so if used in the udf, the main script won't be able to use them.

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Like careca said: Use Local.

Inside myudf.au3, put Local $myvariable3.
Then, in the script

Msgbox(default,"special variable",$myvariable3)

Will give you an error, because $myvariable3 is out of scope.

Then, if you want to use it, you will need a function to return the value to your main script, E.g:
myudf.au3:

Func getMyvariable3()
    Return $myvariable3
EndFunc

mainscript.au3:

Msgbox(default, "special variable", getMyvariable3())

 

Link to comment
Share on other sites

Static declaration is useful too: when all your dreams can come true! :)

Global Const $vCurrentValue = 10 ; Cannot be modified

Func CurrentValue($bReturn = False)
    ; declare a local variable with the same name (no conflict with the global value)
    Local Static $vCurrentValue = 0 ; Static variables retain memory in local scope

    If $bReturn Then
        Return $vCurrentValue ; either return the value without modification
    Else
        $vCurrentValue += 1 ; OR modify the variable (this is just an example)
    EndIf
EndFunc

While CurrentValue(True) < $vCurrentValue
    CurrentValue() ; first, this will modify the value
    ConsoleWrite("$vCurrentValue = " & CurrentValue(True) & @LF) ; secondly, return the value without modification (as demo)
WEnd

 

Edited by czardas
Link to comment
Share on other sites

Can't help with that. And i fail to see what is the problem to have the variables available in the script too.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

It's possible to hide the variable's value in the main script, like this:

Global $test=123

_Main()

Func _Main()

    Local $test=""
    MsgBox(0,"var $test in Main",$test)
    _MyUDF1()

    MsgBox(0,"var $test in Main",$test)
    _MyUDF2()

    MsgBox(0,"var $test in Main",$test)
    _MyUDF2()

EndFunc

Func _MyUDF1()
    MsgBox(0,"var $test in _MyUDF1",$test)
EndFunc

Func _MyUDF2()
    MsgBox(0,"var $test in _MyUDF2",$test)
EndFunc

Func _MyUDF3()
    MsgBox(0,"var $test in _MyUDF3",$test)
EndFunc

 

Link to comment
Share on other sites

2 hours ago, Surya said:

i want the variable to be available within all the functions of the UDF not the only function but not in the mainscript

It is available anywhere in the script, but you need to call the function. I would have written it slightly differently, but this is just an example. Here are four extra lines which demonstrate my point. You may want to send a new value by changing the parameters for the function. I don't have time ATM, but this is very easy to do.

Global Const $vCurrentValue = 10 ; Cannot be modified

Func CurrentValue($bReturn = False)
    ; declare a local variable with the same name (no conflict with the global value)
    Local Static $vCurrentValue = 0 ; Static variables retain memory in local scope

    If $bReturn Then
        Return $vCurrentValue ; either return the value without modification
    Else
        $vCurrentValue += 1 ; OR modify variable (this is just an example)
    EndIf
EndFunc

While CurrentValue(True) < $vCurrentValue
    CurrentValue() ; will modify the value
    ConsoleWrite("$vCurrentValue = " & CurrentValue(True) & @LF) ; returns the value without modification
WEnd

SomewhereOverTheRainbow()

Func SomewhereOverTheRainbow()
    MsgBox(0, "Got It!", CurrentValue(True))
EndFunc

 

Link to comment
Share on other sites

5 hours ago, Surya said:

i want the variable to be available within all the functions of the UDF not the only function but not in the mainscript

This isn't possible, AutoIt doesn't have a File scope, just local (within a function) and Global (available everywhere).

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

This is what I was referring to earlier:

StoredValue(1 + 1) ; write
MsgBox(0, "", StoredValue()) ; read

StoredValue(StoredValue() ^ 5) ; write
MsgBox(0, "", StoredValue()) ; read

Func StoredValue($vVal = 0)
    Local Static $vStoredValue
    If @NumParams Then $vStoredValue = $vVal
    Return $vStoredValue
EndFunc

The local variable $vStoredValue is invisible but can be modified, or read, by calling the function StoredValue(). That's about as good as it gets without File Scope, which @BrewManNH just mentioned.

Link to comment
Share on other sites

My first question would be why? Whats the problem if its accessible in mainscript

But a solution could be use autoitobject internal. And make an object with same name as your udf and then use udf.varname

See examples thread for the udf.

alternative

Read isdeclared function and assign function and use in udf variables declared thru assign without a $prefix.

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