Jump to content

To skip code if it is a library


ur
 Share

Recommended Posts

Is there any way to skip the execution of a particular block of code like any condition or expression, if the au3 file is included as library.?

Like, in my code I wrote all the functionality as functions and called them in 4 lines.

Now I have another requirement where I need to use the same code but different.So I am importing this code so that I can use these functions.

But the code block which is outside the functions (main code) is being executed when I import the au3 file.Is there any condition to check whether the file is running directly or included in another au3 file as library, so that I can keep the same here.

 

Please suggest if any ideas.

Link to comment
Share on other sites

  • Moderators

ur,

Code from an included file is taken verbatim into the main script at the point of the #include directive, so there is no way of directly determining if the code comes from a library or not when the script is running.

If you were a little clearer in your description we might be able to offer some ideas to help - can you show at least some pseudo-code to indicate exactly what you are trying to do.

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

You should not have main code in included scripts, but instead put that main code in a function too, so you can decide whether to call it or not. You should have as much code as possible in functions anyway, even in your main script, for the same reason.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

In one file I have written complete logic to build ism files in functions and in the main code.I am calling two custom functions written.

build32();this custom function will compile one particular set of ism 32bit files.
build64();this custom function will compile one particular set of ism 64bit files.

Now, different requirement came where the user should have option to select the ism files(around 9 are there and user will select in the GUI as checkboxes), so I wrote separate au3 file for the selection and calling the above functions based on his selection in GUI.The above lines are in main code should not be executed when I call this in library because I am calling them in the new file based on the user selection.

I don't want to completely remove the lines from main code as we are using that file in different scenario also.If I keep a separate copy of each, then I need to change it every time in both places.Instead, is there any way to surround the above two lines with a if condition.

I tried by keeping a global variable (true or false) and changing it to false in the new code.But it is not taking.

Let me if the files are required, I will attach them.

Link to comment
Share on other sites

The thing is that "main" code (code outside of functions) will ALWAYS be executed if/when you include the script. To make your include file more versatile, move that "main" code into a function, or multiple functions, and only call the relevant functions when necessary. That also allows you to have the code inside the function to work with parameters.

If that is somehow not what you need then I guess I'm missing your point. Ideally, write a small reproducer to everyone knows what you are actually doing, or else (indeed) post your code and indicate what's going wrong where.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Hi Bunny,

I have written all the logic to individual functions and calling the below two functions in the main code.

build32();this custom function will compile one particular set of ism 32bit files.
build64(

This main code we are using it for our regular activities.

Now as a separate task, we are creating GUI for selection of particular items and included this main code as library in that GUI code.

But these two functions are getting called in that also as you said.And we can't remove these 2 lines as it will stop the regular task.

So, if possible so that we can surround the above lines with condition so that it will execute in main task and in the GUI it won't.

Please suggest.

Link to comment
Share on other sites

  • Moderators

ur,

Looking at those files, I suggest doing as SadBunny has suggested above: placing the GUI creation code you currently have as the "main script" element of guibuild.au3 into a separate function which you can then call when required.

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

Seems like an ideal use of the IsDeclared function.

Declare a variable in your additional au3, say $additional

in your main script something like ...

If Not IsDeclared("additional")
    build32()
Endif

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I tried by creating a variable in the GUI logic file and used the logic by JohnOne

If Not IsDeclared("additional")

But it is taking the variable as undeclared.

So I created the variable on the line above the include statement.

Now it is working...

All of you guys, Thank you very much.

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

×
×
  • Create New...