Basic question about methods
Basic question about methods
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!
- lumberjack
- Posts: 727
- Joined: Fri Sep 25, 2015 3:11 pm
- Location: South Africa
Basic question about methods
Hi Jeff,
Functions are STATIC METHODS so they are class methods and are not part of objects created from the class.
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.
See the documentation for the short versions of PROPERTY / END PROPERTY
Yes there is a big difference.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.
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
Code: Select all
PROPERTY MyProp AS STRING
GET // Access
RETURN SELF:_myprop
END GET
SET // Assign
SELF:_myprop := VALUE
END SET
END PROPERTY
______________________
Johan Nel
Boshof, South Africa
Johan Nel
Boshof, South Africa
Basic question about methods
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.
There are always many ways to do the things. The Question is what is the best to achieve your goals.
Stefan
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
Stefan
Basic question about methods
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
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
XSharp Development Team
fabrice(at)xsharp.eu
fabrice(at)xsharp.eu
Basic question about methods
Thanks for the guidance everyone, it's been extremely helpful!