From: Oliver S. <oli...@si...> - 2011-12-29 23:48:56
|
Hi Eric, Don't know if I can help, but I agree that using attributes is not quite as clear in the documentation as it might be (although there's an example of validation on page 82 of the ooRexx Reference). Now I don't claim to be an expert on attributes, but I do know that OO languages/software have many ways of implementing the attribute concept. The ooRexx approach, once you get used to it, is imho rather nice. The following code aims to provide some examples of using attributes - both defining them in a class called "MainlyAttributes" and getting/setting then from outside the class. Each of the three attributes in the class is defined slightly differently to indicate the range of options available. Hope this helps. /* Check out Attributes A class with three attributes - x, y, and z, each specified differently: x is fully spec'd using the ::attribute directive for both get and set. Note the "set" code provided with the ::attribute directive. This code constrains the value to be a numeric whole positive number and is taken from the ooRexx Reference (page 82). y uses the ::attribute statement for gets only; the set is provided as a specific method called "y=". This method checks for numeric and returns either .true or .false. Note that the returned value must be checked using the "result" special variable. If ,false is returned, the value of y is set to the string "Not Set". z uses a single ::attribute directive with default get/set bevavior. */ i = .MainlyAttributes~new say; say "-----------------Attributes Test----------------------------" -- Get initial values of all three attributes: say "Initial values of attributes assigned in the 'init' method:" say "x=" i~x " y=" i~y " z=" i~z say; say "Set x to 123 with the statement 'i~x = 123'." i~x = 123 say "x is now:" i~x say; say "Invoke a method that 'gets' all three attributes:" i~getAllThree say; say "Set y to an invalid value ..." say "Invalid: i~y='abc'" i~y='abc' say "Result =" result say "... and now to a valid value:" say "Valid: i~y=100" i~y=100 say "Result =" result say; say "Set z to a different value using i~z='xyz'" i~z='xyz' say "z =" i~z say; say "Set x to a valid numeric value using i~x=123" i~x = 123; say "i~x =" i~x say; say "Set x to a non-numeric value. This will raise an error and exit." say "But it's the last example in this program, so an exit here is OK." i~x = "abc" -- ---------------------------------------------------------------------------- - ::class MainlyAttributes ::attribute x get ::attribute x set expose x use arg value -- Prevent x having a value that is not a whole number by throwing an error -- and exiting if x is assigned a bad value: if datatype(value, "Whole") = .false | value < 0 then raise syntax 93.906 array ("x", value) x = value return 1 ::attribute y get ::attribute z ::method init expose x y z x = 0 y = "" z = "abc" ::method getAllThree expose x y z say "x =" x say "y =" y say "z =" z ::method "y=" -- equivalent to ::attribute y set expose y use arg a if a~datatype("N") then do y = a return .true end y = "" return .false -- ---------------------------------------------------------------------------- - _____ From: Eric Davidson [mailto:eri...@bi...] Sent: 28 December 2011 16:32 To: oor...@li... Subject: [Oorexx-users] ::attribute validation As an ex-mainframe person who has been using REXX extensively for over 20 years, and using ooRexx procedurally for the last 5. I am trying to update myself to develop code using the OO techniques. I have read the documentation and I believe I understand the basics of methods, but I am currently stuck using attributes - specifically how to validate the values an attribute can have. I can't seem to find simple examples of creating methods and validating. Apologies if this is a basic question that I should have referred elsewhere Eric Davidson |