Thanks to @RhoSigma , this wonderful keyword was added into the language a few versions back. I'm honestly surprised we haven't had more people talk about it, use it, or at least ask about it! This is one of those commands that I consider to be a *MUST HAVE* for anyone who does any serious library creation.
What is $INCLUDEONCE? And what's it do for us that's so nifty?
$INCLUDEONCE is a metacommand that you place inside any of your library files, and no matter how many $INCLUDE statements they show up in, they're only included ONCE in your code.
For example, let's say I write various libraries and I have a Truth.BI file which I make excessive use of:
Truth.BINow, I write a little program which saves files for me to the disk as *.SSF (Steve Special Format) files. This save file library is going to $INCLUDE:'Truth.BI.
And I also write a little program which loads files for me from the disk, if they're *.SFF files. This is seperate from the save library as folks may just need one of the routines and not both in their code. This file would also have in it somewhere the need for $INCLUDE:'Truth.BI'
Now, for my own use, I want to use *both* of the routines in my program -- I want to both save and load files. So, I write my code for the main program to look something like the following:
And, in the IDE, I get warnings and errors and dancing bears growling and trying to eat me as I'm now trying to include Truth.BI twice in my code and that means I'm trying to define the same CONSTs multiple times. QB64PE doesn't like that type of behavior!!
So what's the simple fix??
Let's go back and change Truth.Bi just a little:
Now, even though that library "Truth.BI" is included in multiple other libraries, and thus included in my code at multiple points, IT'S ONLY ADDED ONCE!!
The first time a file with $INCLUDEONCE in it is $INCLUDEd into your code, it's added to the code. At each and EVERY point after that, that code is just skipped as you've already included it into your code earlier!!
$INCLUDEONCE, when placed inside a bas/bi/bm file, makes certain that the code in that file is only included one time, no matter how many $INCLUDE statements try to reference that file.
And that's a game changer right there, for anyone who writes and creates library files!
For everyone else, if you don't use $INCLUDE, you can basically just save the brain cells and forget this command even exists. It ONLY affects $INCLUDE behavior, and does absolutely nothing in any other type code.
What is $INCLUDEONCE? And what's it do for us that's so nifty?
$INCLUDEONCE is a metacommand that you place inside any of your library files, and no matter how many $INCLUDE statements they show up in, they're only included ONCE in your code.
For example, let's say I write various libraries and I have a Truth.BI file which I make excessive use of:
Truth.BI
Code: (Select All)
CONST True = -1
CONST False = 0
And I also write a little program which loads files for me from the disk, if they're *.SFF files. This is seperate from the save library as folks may just need one of the routines and not both in their code. This file would also have in it somewhere the need for $INCLUDE:'Truth.BI'
Now, for my own use, I want to use *both* of the routines in my program -- I want to both save and load files. So, I write my code for the main program to look something like the following:
Code: (Select All)
$INCLUDE:'SaveSSF.BI'
$INCLUDE:'LoadSSF.BI'
... more code
So what's the simple fix??
Let's go back and change Truth.Bi just a little:
Code: (Select All)
$INCLUDEONCE
CONST True = -1
CONST False = 0
The first time a file with $INCLUDEONCE in it is $INCLUDEd into your code, it's added to the code. At each and EVERY point after that, that code is just skipped as you've already included it into your code earlier!!
$INCLUDEONCE, when placed inside a bas/bi/bm file, makes certain that the code in that file is only included one time, no matter how many $INCLUDE statements try to reference that file.
And that's a game changer right there, for anyone who writes and creates library files!
For everyone else, if you don't use $INCLUDE, you can basically just save the brain cells and forget this command even exists. It ONLY affects $INCLUDE behavior, and does absolutely nothing in any other type code.