is it possible to do this?
is it possible to do this?
it goes something like this.
one program, plus multiple compiled or source files.
6 different files, have the same functions but are different inside.
if you were to include A.BI or something like that instead of B.BI, you get 333 as the result by the "PrintNumber" command.
Using B.BI you will get 222 as the result.
Because (name).BI assigns shared dim variable, NUM to a certain integer.
Example:
Dim Shared A as Integer
Dim B as Integer
Input "Enter the file number.", B
Select Case B
Case 1
use "A.BI" 'Or a compiled file.
case 2
use "B.BI"
End Select
PrintNumber
Print A
the USE command is not real unless if there is a way to do just that.
In A.BI
Sub PrintNumber()
A=333
End Sub
In B.BI
Sub PrintNumber()
A=222
End Sub
i dunno if anyone has done something similar to this or not.
one program, plus multiple compiled or source files.
6 different files, have the same functions but are different inside.
if you were to include A.BI or something like that instead of B.BI, you get 333 as the result by the "PrintNumber" command.
Using B.BI you will get 222 as the result.
Because (name).BI assigns shared dim variable, NUM to a certain integer.
Example:
Dim Shared A as Integer
Dim B as Integer
Input "Enter the file number.", B
Select Case B
Case 1
use "A.BI" 'Or a compiled file.
case 2
use "B.BI"
End Select
PrintNumber
Print A
the USE command is not real unless if there is a way to do just that.
In A.BI
Sub PrintNumber()
A=333
End Sub
In B.BI
Sub PrintNumber()
A=222
End Sub
i dunno if anyone has done something similar to this or not.
-
- Posts: 5494
- Joined: Sep 12, 2005 20:06
- Location: California
You can use namespaces for something like that instead, so you can just use the a.PrintNumber() namespace, or the b.PrintNumber namespace. You could also put those functions in a TYPE and use them as methods:
Namespaces:
Methods:
OR!, the very closest thing to what you are thinking is to use DLLs and load that code dynamically. The thing is, #include'ing is done on the PREPROCESSOR, so all that information is known before the code's even done COMPILING, so it can't be changed at RUNTIME.
However, if you use DLLs, you can load this code at RUNTIME and change which function to use simply by loading it from the DLL File. The FreeBASIC wiki has more information on DLLs.
Namespaces:
Code: Select all
namespace a
function printNumber() as integer
return 222
end function
end namespace
namespace b
function printNumber() as integer
return 333
end function
end namespace
Code: Select all
type a
'' we don't actually use the field member 'number' It's just a filler
'' because types can't just have functions.
number as integer
declare function printNumber() as integer
end type
function a.printNumber() as integer
return 222
end function
type b
number as integer
declare function printNumber() as integer
end type
function b.printNumber() as integer
return 333
end function
dim as a myA
dim as b myB
print myA.printNumber()
print myB.printNumber()
sleep
However, if you use DLLs, you can load this code at RUNTIME and change which function to use simply by loading it from the DLL File. The FreeBASIC wiki has more information on DLLs.
-
- Posts: 252
- Joined: Mar 12, 2006 16:25
Using dll's
Look at this Post:
http://www.freebasic.net/forum/viewtopi ... 2219#82219
http://www.freebasic.net/forum/viewtopi ... 2219#82219
probably cause he knows how to use arrays and is not familiar with that method. i can honestly say im with him on that.
you guys need to think more on a novice level your wisdom is great but you leave far to many fill in the blanks... and to say thats wrong do it this way isnt exactly beneficial advice to someone who has never done it your way.
im not knockin yah Chaos as i said your knowledgable, but you think UP here when others are thinking down here. im sure im not the only noob with that complaint.
you guys need to think more on a novice level your wisdom is great but you leave far to many fill in the blanks... and to say thats wrong do it this way isnt exactly beneficial advice to someone who has never done it your way.
im not knockin yah Chaos as i said your knowledgable, but you think UP here when others are thinking down here. im sure im not the only noob with that complaint.
Oh I see. I could write a code example but... you can't use shared vars like that from within a shared library. The shared lib will get its own version of the variable. You'd have to pass a reference of some sort to the actual proc. Here's a functional example:
First create these 4 files in the same directory, and name them as I tell you
libfuncs.bas
dylib.bi
dylib.bas
test.bas
Now, go to a command prompt inside the directory you created the files. Issue these commands:
Now, run test.exe. Since some_func0 and some_func1 are defined, you can enter 0 or 1 at the prompt. Hope you can learn from it.
First create these 4 files in the same directory, and name them as I tell you
libfuncs.bas
Code: Select all
'' the 'export's are necessary
''
'' also, FreeBASIC will make these names CAPS, so remember that.
sub some_func0( byref i as integer ) export
i = 222
end sub
sub some_func1( byref i as integer ) export
i = 333
end sub
Code: Select all
#pragma once
type DYLIB_ARRAY
lib_core as any ptr
declare constructor( byref lib_ as string )
declare destructor( )
declare function proc _
( _
byval proc_num as integer _
) as any ptr
end type
Code: Select all
'' see the note in libfuncs.bas about caps
const as string PROC_ID = "SOME_FUNC"
#include "dylib.bi"
'' called when a var is dimmed
constructor DYLIB_ARRAY( byref lib_ as string )
lib_core = dylibload( lib_ )
end constructor
'' called when a var dies
destructor DYLIB_ARRAY( )
dylibfree( lib_core )
end destructor
function DYLIB_ARRAY.proc _
( _
byval proc_num as integer _
) as any ptr
'' get the function, if proc is 2, then it's looking for "some_func2"
function = dylibsymbol( lib_core, PROC_ID & proc_num )
end function
Code: Select all
#include "dylib.bi"
dim as DYLIB_ARRAY my_lib = "libfuncs"
dim as integer p, a
Input "Enter the proc number.", p
'' notice this matches what's in libfuncs.bas
dim as sub( byref i as integer ) my_proc
'' like an array?
my_proc = my_lib.proc(p)
'' 0 means it wasn't found
if( my_proc = 0 ) then
print "I couldn't load that proc"
else
'' call it like a sub
my_proc( a )
end if
print a
sleep
Code: Select all
fbc libfuncs.bas -dylib
fbc test.bas dylib.bas