Class Libraries
You can now create DLLs containing classes as well as SUBs an Functions. They are declared in much the same way:
class greeting lib `chatter.dll` alias `greeting`
followed by the rest of the class header.
On the library side, the classes must be marked export
class greeting alias `greeting` export
If no Alias is specified then the default is a lowercase match for the class name used.
Internally the methods table is passed from the library to the client at load time. This ensures that new classes can be derived from the the library classes.
The script below first generates a miinimal class library chatter.dll containing one class with 2 methods. The second parts of the script creates an object with it.
Oxygen Update: http://community.thinbasic.com/index.php?topic=2517
[code=thinbasic]
uses "oxygen","file"
dim src as string
'
'
'
' ###############
' ###################
' ### ###
' ### ### ###
' ### ## ## ###
' ### ## ## ###
' ### ## ## ###
' ### ## ## ###
' ### ## ## ###
' ### #### ## ###
' ### #### ###
' ### ###
' ### ###
' #############
' #########
'----------------------
'GENERATE CLASS LIBRARY
'======================
src ="
#basic
#file `chatter.dll`
'-------------------------------------
class greeting alias `greeting` export
'=====================================
method hello(n as string)
;
method goodbye(n as string)
/
a as long
end class
methods of greeting
method hello(n as string)
print `Hello ` n
end method
method goodbye(n as string)
print `Goodbye ` n
end method
end methods of greeting
sub finish()
terminate
end sub
"
'msgbox 0,o2_prep src
o2_basic src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec
' ##### ###### ###### ###### ######
' ######## ######### ######### ######### ###-*-####
' ## ## ## ## ## ## ## ## #########
' ## ## ## ## ## ## ## ## ##
' ## ## ## ## ## ## ## ## ##
' ## ## ## ## ## ## ## ## ##
' ## ## ## ## ## ## ## ## ##
' ## ## ## ## ## ## ## ## ##
' ## ########## ########## ########## ##########
' # ###### ###### ###### ######
'
'----------------------
'TEST THE CLASS LIBRARY
'======================
src="
#basic
'------------------------------------------------
class greeting lib `chatter.dll` alias `greeting`
'================================================
method hello(n as string)
method goodbye(n as string)
/
a as long
end class
dim g as greeting
g.goodbye `moon`
g.hello `earth`
xit:
terminate
"
file_save ("t.txt",o2_view src )
'msgbox 0,o2_prep src
o2_basic src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec
'msgbox 0,o2_error
[/code]
Bookmarks