fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Basic question about methods

Basic question about methods 05 Jun 2019 17:59 #9212

  • Jeff's Avatar

  • Jeff

  • Topic Author


  • Posts: 2
  • 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!

    Please Log in or Create an account to join the conversation.

    Basic question about methods 06 Jun 2019 09:03 #9218

  • lumberjack's Avatar

  • lumberjack


  • Posts: 680
  • 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.
    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.
    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
    ______________________
    Johan Nel
    George, South Africa

    Please Log in or Create an account to join the conversation.

    Basic question about methods 06 Jun 2019 09:32 #9219

  • SHirsch's Avatar

  • SHirsch


  • Posts: 163
  • 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.
    //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

    Please Log in or Create an account to join the conversation.

    Basic question about methods 06 Jun 2019 10:53 #9220

  • Fabrice's Avatar

  • Fabrice


  • Posts: 70
  • 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

    Please Log in or Create an account to join the conversation.

    Last edit: by Fabrice.

    Basic question about methods 06 Jun 2019 23:04 #9221

  • Jeff's Avatar

  • Jeff

  • Topic Author


  • Posts: 2
  • Thanks for the guidance everyone, it's been extremely helpful!

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1