Excluding Library Code
#1
If you guys are like me, and code everything into library snippets to reuse it over and over, you probably end up finding that some of your essential code ends up going into multiple libraries and then tossing errors when you make use of those libraries together.

Here's a solution which works in this case:




Code: (Select All)
$IF EXT = UNDEFINED THEN
    $LET EXT = TRUE
    FUNCTION ExtendedTimer##
    DIM m AS INTEGER, d AS INTEGER, y AS INTEGER
    DIM s AS _FLOAT, day AS STRING
    day = DATE$
    m = VAL(LEFT$(day, 2))
    d = VAL(MID$(day, 4, 2))
    y = VAL(RIGHT$(day, 4)) - 1970
    SELECT CASE m 'Add the number of days for each previous month passed
    CASE 2: d = d + 31
    CASE 3: d = d + 59
    CASE 4: d = d + 90
    CASE 5: d = d + 120
    CASE 6: d = d + 151
    CASE 7: d = d + 181
    CASE 8: d = d + 212
    CASE 9: d = d + 243
    CASE 10: d = d + 273
    CASE 11: d = d + 304
    CASE 12: d = d + 334
    END SELECT
    IF (y MOD 4) = 2 AND m > 2 THEN d = d + 1 'add a day if this is leap year and we're past february
    d = (d - 1) + 365 * y 'current month days passed + 365 days per each standard year
    d = d + (y + 2) \ 4 'add in days for leap years passed
    s = d * 24 * 60 * 60 'Seconds are days * 24 hours * 60 minutes * 60 seconds
    ExtendedTimer## = (s + TIMER)
    END FUNCTION
$END IF

ExtendedTimer is something which I use in a lot of code, and as such, it gets included into a lot of libraries. By coding it like this, QB64 only includes it in my programs once -- no matter how many libraries it's contained within -- and doesn't toss me "Name already in use" errors and whatnot."

It's a simple enough little habit to get into using with our library code, and it'll prevent the issues with duplicate copies being in multiple libraries.
Reply




Users browsing this thread: 3 Guest(s)