Page 1 of 1
Basic question about methods
Posted: Wed Jun 05, 2019 3:59 pm
by Anonymous
Hi, I'm working with X# for the first time and had a simple question. Is there a difference between METHOD and FUNCTION? Also, I understand the difference between ASSIGN and ACCESS but are these just an alternative to a classic method, or is there another reason for these? I feel like I'm seeing four different ways of doing the same thing. Thanks!
Basic question about methods
Posted: Thu Jun 06, 2019 7:03 am
by lumberjack
Hi Jeff,
Jeff wrote:Is there a difference between METHOD and FUNCTION? Also, I understand the difference between ASSIGN and ACCESS but are these just an alternative to a classic method, or is there another reason for these? I feel like I'm seeing four different ways of doing the same thing.
Yes there is a big difference.
Functions are STATIC METHODS so they are class methods and are not part of objects created from the class.
Code: Select all
CLASS MyClass
METHOD MyMethod() AS VOID
? "Blah blahh"
RETURN
STATIC METHOD StatMethod() AS VOID
? "Static method"
RETURN
END CLASS
VAR o := MyClass{}
?o:MyMethod()
?o:StatMethod() // Compile error does not contain a method StatMethod()
?MyClass.StatMethod() // Correct way, it is a class method not an object method
Access/Assign is the VO way of properties. Robert did an implementation to have that wrapped in a PROPERTY, property is the preferred way of doing it.
Code: Select all
PROPERTY MyProp AS STRING
GET // Access
RETURN SELF:_myprop
END GET
SET // Assign
SELF:_myprop := VALUE
END SET
END PROPERTY
See the documentation for the short versions of PROPERTY / END PROPERTY
Basic question about methods
Posted: Thu Jun 06, 2019 7:32 am
by SHirsch
Hi Jeff,
METHOD belongs to a CLASS.
FUNCTION can be declared outside any CLASS definition. Internally it is a STATIC METHOD of a STATIC CLASS Functions.
Code: Select all
//enhanced sample from lumberjack, to show ACCESS, ASSIGN and FUNCTION
CLASS MyClass
METHOD MyMethod() AS VOID
? "Blah blahh"
RETURN
STATIC METHOD StatMethod() AS VOID
? "Static method"
RETURN
PRIVATE _prop:=23 AS INT
ACCESS MyProp as INT
RETURN SELF:_prop
ASSIGN MyProp(n as INT) VOID
SELF:_prop := n
RETURN
END CLASS
FUNCTION MyFunc() AS VOID
?"Function"
RETURN
VAR o := MyClass{}
?o:MyMethod()
?o:StatMethod() // Compile error does not contain a method StatMethod()
?MyClass.StatMethod() // Correct way, it is a class method not an object method
?var x:= o:MyProp //x is now 23
?o:MyProp := 42 //now _prop is 42
?MyFunc() //call a function with out any reference to a class
There are always many ways to do the things. The Question is what is the best to achieve your goals.
Stefan
Basic question about methods
Posted: Thu Jun 06, 2019 8:53 am
by Fabrice
Hi Jeff,
Function or Method is a matter of design :
Are you Data Centric or "Action" centric ?
In the first case, you concentrate on the Data, so you identify the Objects of the system, then design the Classes that will provide these objects.
As Objects have States (Properties/Fields), they have Actions related "only" to these (objects) : These are the Methods.
If you are Action-centric, you will search for the treatments, datas are then parameters and returns
Sometimes, in the Data-Centric model, you identify some treatments that are not-really related to objects (or you don't want to go that deeper in the design), so you use a "Static Method", or a Function in xBase terminology.
Now, Access and Assign are a kind-of Methods that allows Read/Write access to Fields, or that are used to create "virtual" fields :
Think about a Rectangle class, that has Width and Height Fields, but Area is a virtual field calculated each time, it's a good candidate for an Access.
More generally, in OOD, we talk about Getter and Setter, which you can find in xSharp Core, or Vulcan dialects, with Property Get and Property Set.
HTH,
Fab
Basic question about methods
Posted: Thu Jun 06, 2019 9:04 pm
by Jeff
Thanks for the guidance everyone, it's been extremely helpful!