ScriptBasic > Tutorials

ScriptBasic OOP

<< < (3/3)

Support:
Here is an example of extending the customer record with a shipto address block within the user code. I prefixed all module interface structures with the underscore character to specify property elements from the object interface.


--- Code: ---' ScriptBasic OOP Example

GLOBAL CONST NL = "\n"

MODULE Customer

IMPORT t.bas

instance = 0

_phone_num{"shop"} = "800-THE-SHOP"
_phone_num{"cell"} = "900-OUR-CELL"

_age_bal{"current"} = 0
_age_bal{"30"} = 0
_age_bal{"60"} = 0
_age_bal{"90"} = 0
_age_bal{"overdue"} = 0

_cust_rec{"name"} = "John Doe"
_cust_rec{"addr1"} = "123 St."
_cust_rec{"addr2"} = "Anytown,USA"
_cust_rec{"phone"} = _phone_num
_cust_rec{"aging"} = _age_bal

obj[0] = _cust_rec

FUNCTION New(base)
  instance += 1
  IF base = undef THEN
    obj[instance] = obj[0]
  ELSE
    REF obj[instance] = obj[base]
  END IF
  new = instance
END FUNCTION

FUNCTION Inherit(base)
  instance += 1
  obj[instance] = obj[base]
  Inherit = instance
END FUNCTION

FUNCTION ObjDef
  ObjDef = t::ArrayToXML(_cust_rec)
END FUNCTION

FUNCTION Destroy(object)
  obj[object] = undef
END FUNCTION

END MODULE

'
' MAIN
'

cust1 = customer::New()

customer::_ship_to{"name"} = customer::obj[cust1]{"name"}
customer::_ship_to{"addr1"} = customer::obj[cust1]{"addr1"}
customer::_ship_to{"addr2"} = customer::obj[cust1]{"addr2"}

customer::_cust_rec{"shipto"} = customer::_ship_to
customer::obj[cust1] = customer::_cust_rec

PRINT customer::obj[cust1]{"shipto"}{"name"},NL
PRINT customer::obj[cust1]{"shipto"}{"addr1"},NL
PRINT customer::obj[cust1]{"shipto"}{"addr2"},NL
PRINTNL

PRINT customer::ObjDef()

customer::Destroy(cust1)

--- End code ---

Results:

--- Code: ---C:\scriptbasic\test>oop2ep
John Doe
123 St.
Anytown,USA

<?xml version="1.0" encoding="UTF-8"?><V><A l="0" h="11"><S>name</S><S>John Doe</S><S>addr1</S><S>123 St.</S><S>addr2</S><S>Anytown,USA</S><S>phone</S><A l="0" h="3"><S>shop</S><S>800-THE-SHOP</S><S>cell</S><S>9
00-OUR-CELL</S></A><S>aging</S><A l="0" h="9"><S>current</S><I>0</I><S>30</S><I>0</I><S>60</S><I>0</I><S>90</S><I>0</I><S>overdue</S><I>0</I></A><S>shipto</S><A l="0" h="5"><S>name</S><S>John Doe</S><S>addr1</S>
<S>123 St.</S><S>addr2</S><S>Anytown,USA</S></A></A></V>
C:\scriptbasic\test>

--- End code ---



The next installment will show extending module methods from user code.

Support:
Here is my first pass at extending object methods in user code. This example shows extending the interface properties and copying the the customer main address to the new shipto address block with an extended method call. This allows calling the user extended method function in user code or by the module. This is work in progress so things might change.

This example also shows how encapsulation can be done. In a user method, multiple objects references can be encapsulated with one call to the ObjExt() method of the object.



--- Code: ---' ScriptBasic OOP Example

GLOBAL CONST NL = "\n"

MODULE Customer

IMPORT t.bas

instance = 0

_phone_num{"shop"} = "800-THE-SHOP"
_phone_num{"cell"} = "900-OUR-CELL"

_age_bal{"current"} = 0
_age_bal{"30"} = 0
_age_bal{"60"} = 0
_age_bal{"90"} = 0
_age_bal{"overdue"} = 0

_cust_rec{"name"} = "John Doe"
_cust_rec{"addr1"} = "123 St."
_cust_rec{"addr2"} = "Anytown,USA"
_cust_rec{"phone"} = _phone_num
_cust_rec{"aging"} = _age_bal

obj[0] = _cust_rec

FUNCTION New(base)
  instance += 1
  IF base = undef THEN
    obj[instance] = obj[0]
  ELSE
    REF obj[instance] = obj[base]
  END IF
  new = instance
END FUNCTION

FUNCTION Inherit(base)
  instance += 1
  obj[instance] = obj[base]
  Inherit = instance
END FUNCTION

FUNCTION ObjExt(fna,arg1,arg2,arg3)
  LOCAL r
  r = ICALL(fna,arg1,arg2,arg3)
END FUNCTION

FUNCTION ObjDef
  ObjDef = t::ArrayToXML(_cust_rec)
END FUNCTION

FUNCTION Destroy(object)
  obj[object] = undef
END FUNCTION

END MODULE

'
' MAIN
'

FUNCTION Copy2Ship(custobj)
  customer::obj[custobj]{"shipto"}{"name"} = customer::obj[custobj]{"name"}
  customer::obj[custobj]{"shipto"}{"addr1"} = customer::obj[custobj]{"addr1"}
  customer::obj[custobj]{"shipto"}{"addr2"} = customer::obj[custobj]{"addr2"}
END FUNCTION

updship = ADDRESS(Copy2Ship())

cust1 = customer::New()

customer::_ship_to{"name"} = ""
customer::_ship_to{"addr1"} = ""
customer::_ship_to{"addr2"} = ""

customer::_cust_rec{"shipto"} = customer::_ship_to
customer::obj[cust1] = customer::_cust_rec

customer::ObjExt(updship,cust1)

PRINT customer::obj[cust1]{"shipto"}{"name"},NL
PRINT customer::obj[cust1]{"shipto"}{"addr1"},NL
PRINT customer::obj[cust1]{"shipto"}{"addr2"},NL
PRINTNL

customer::Destroy(cust1)

--- End code ---

C:\scriptbasic\test>oop2me
John Doe
123 St.
Anytown,USA

C:\scriptbasic\test>

Support:
This concludes the ScriptBasic OOP framework code project/tutorial from an introduction standpoint.

If you could respond to a few questions I have then it would help me fine tune and expand on ScriptBasic OOP.

Q. Do you see an advantage of using OO style programming with your projects?

Q. Is this code project tutorial enough to get you started using ScriptBasic OOP?

Q. If you have used OOP with other languages, do you see this implementation easy to use?

Q. What do you see as missing in this implementation of OOP?

Navigation

[0] Message Index

[*] Previous page

Go to full version