Amiga E OOP Tutorial - Example Code
MODULE 'tools/exceptions'
-> This constants are used later in get() and memorize()
ENUM MEMORIZE_NAME=0, MEMORIZE_SURNAME
/*
Here there is our "person" class
*/
OBJECT person
PRIVATE -> All data are private
name:PTR TO CHAR
surname:PTR TO CHAR
ENDOBJECT
PROC person() OF person -> person object constructor
self.name:=String(30)
IF self.name = NIL THEN Raise("MEM")
self.surname:=String(50)
IF self.surname = NIL THEN Raise("MEM")
ENDPROC
PROC end() OF person -> person object destructor
DisposeLink(self.name)
DisposeLink(self.surname)
ENDPROC
PROC clear() OF person -> here we clears the strings
StrCopy(self.name, '')
StrCopy(self.surname, '')
ENDPROC
/*
This proc is used to memorize data inside the object.
Please, note the SELECT mode, which allows easy object
expansion.
*/
PROC memorize(v, mode=MEMORIZE_NAME) OF person
SELECT mode
CASE MEMORIZE_NAME
StrCopy(self.name, v)
CASE MEMORIZE_SURNAME
StrCopy(self.surname, v)
ENDSELECT
ENDPROC
-> This command returns objects values
PROC get(mode=MEMORIZE_NAME) OF person
DEF p=NIL
SELECT mode
CASE MEMORIZE_NAME
p:=self.name
CASE MEMORIZE_SURNAME
p:=self.surname
ENDSELECT
ENDPROC p
/*
Here starts the ioperson object, a DERIVED class of person
which allows object I/O
*/
OBJECT ioperson OF person
-> We do not have any new data
ENDPROC
PROC ioperson() OF ioperson -> ioperson class CONSTRUCTOR
self.person() -> here we call the person() CONSTRUCTOR
ENDPROC
PROC end() OF ioperson -> ioperson DESTRUCTOR is just the same of person
SUPER self.end() -> Hey a SUPER statement!
ENDPROC
-> This proc loads data from a file inside the ioperson object
PROC load(fname) OF ioperson HANDLE
DEF fh=NIL, buf[256]:STRING
IF (fh:=Open(fname, MODE_OLDFILE))=NIL THEN Raise("file")
FgetS(fh, buf, 255)
StrCopy(self.name, buf)
FgetS(fh, buf, 255)
StrCopy(self.surname, buf)
EXCEPT DO
IF fh THEN Close(fh)
ReThrow()
ENDPROC
-> This proc saves data from a ioperson object to a file.
PROC save(fname) OF ioperson HANDLE
DEF fh=NIL
IF (fh:=Open(fname, NEWFILE))=NIL THEN Raise("file")
FputS(fh, self.name)
FputS(fh, self.surname)
EXCEPT DO
IF fh THEN Close(fh)
ReThrow()
ENDPROC
-> Just a quick and tricky example.
PROC main() HANDLE
DEF iop=NIL:PTR TO ioperson
NEW iop.ioperson()
iop.memorize('Fabio')
iop.memorize('Rotondo', MEMORIZE_SURNAME)
iop.save('Ram:iopTest')
iop.clear()
iop.load('Ram:iopTest')
WriteF('Name:\s - Surname:\s\n', iop.get(), iop.get(MEMORIZE_SURNAME))
EXCEPT DO
report_exception()
END iop
CleanUp(0)
ENDPROC
Main Page
Written By: Fabio Rotondo e-mail: fsoft@intercom.it
C.so Vercelli 9
28100 Novara
ITALY tel: (ITA) - (0)321 459676