Results 1 to 6 of 6

Thread: I wish we had static classes in DataFlex

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2009
    Location
    Stord, Norway
    Posts
    882

    Default I wish we had static classes in DataFlex

    When writing a method, where do you place it? The object oriented approach says: Put it in a class.

    When you want to execute the method, how do you do it? First, you need to instantiate the class by making an object. Then you can call the method by referencing the object. But then what do you do with the object? Do you keep it, so you can call the method at a later time? DataFlex does not have a garbage collector, so if it's not going to be used again, you need to think about destroying the object after using it.

    Why not write a global method? For a simple method that does not have "state" (it is determined solely by its parameters), you could reason that it might as well be written as a global method. And in DataFlex, that does often seem to be the pragmatic way of doing it.
    The downside is that you will then miss out on a number of object oriented features.
    • When debugging, the call stack will not have a useful object to present.
    • When writing unit tests, it is sometimes useful to replace objects with "dummies" (also called mocks, stubs, or fakes) - this can easily be done by changing the object reference; not quite so easily with global methods.
    • Changing behavior by subclassing is not feasible with global methods.


    If the method is going to be called many times, from various places in the code, we sometimes create a single instance of the object, referencing it with a global handle variable. This is a reasonable compromise - we get the benefits of object orientation, and it is trivial to call the method. But you still need to instantiate the object and define the global variable. And you need to carefully keep track of where you put it, because if you try to define it twice, the compiler will complain.

    So what I would really prefer is to be able to define a static class. A static class cannot be instantiated - so when we call its methods, we reference the class itself, instead of an instantiated object.

    Code example:
    Code:
    Class cUtilityClass Static is a cObject
        Procedure DoSomethingUseful
            //... or not...
        End_Procedure
    End_Class
    
    Send DoSomethingUseful of cUtilityClass
    OK, I'll stop writing now
    Last edited by Ola Eldoy; 6-Mar-2017 at 06:48 PM.
    Ola Eldøy (Twitter: @olaeld) - Developer at Emma EDB AS

  2. #2
    Join Date
    Feb 2009
    Location
    Hengelo (NL)
    Posts
    1,891

    Default Re: I wish we had static classes in DataFlex

    Ola,

    I like static classes. They are not available in Dataflex, but you can do it with a trick. As an example, if you have a class dealing with all SQL-stuff. At the end of the package you could add something like:

    Code:
    Procedure CreateStaticSql for Desktop
      Object StaticSQL is a cSql_nameofclass
      End_Object
    End_Procedure
    Send CreateStaticSql of Desktop
    If you add above code to the class-package, then anywhere in your programs you can just 'Send DoSomething of StaticSQL'.

  3. #3
    Join Date
    Feb 2009
    Location
    Stord, Norway
    Posts
    882

    Default Re: I wish we had static classes in DataFlex

    Evertjan, that's a nice way of doing it.

    Here is another alternative that I have been using (placing something like the following at the end of the "static" class package):
    Code:
    //Global variable serves as "static class identifier"Global_Variable Handle gcUtilityClass
    Object oUtilityClass is a cUtilityClass
        Move Self to gcUtilityClass
    End_Object
    Ola Eldøy (Twitter: @olaeld) - Developer at Emma EDB AS

  4. #4
    Join Date
    Feb 2009
    Location
    West Yorkshire - UK
    Posts
    928

    Default Re: I wish we had static classes in DataFlex

    Given that static classes can (by design) have no state, you can use an object referenced by a global_Variable to do this.

    Code:
    global_Variable ghStaticSQL
    object oStaticSQL is a cObject
        Procedure DoSomethingUseful
               //... or not...
        End_Procedure
    
        move (self) to ghStaticSQL
    End_Object
    
    Send DoSomethingUseful to ghStaticSQL
    Sean Bamforth. - D̤͍͍̭̱̄ͦ̆̏̇ͯ͑̄å̩̻͈͒͌t͇̻͙̞̤̱̏̎̐a͈͎͈ͬ̓̽ͮ̓̔̎ͅf̙͓̃̆̈́̔ ̳̣̝͔̲ḷ̩̺̗͎ͤ͂̇̚e̻̙̼̞̥͖̬̹ͮ͌ͫ̆ͬͅx͉̖̣̩̮̖̎͌ ͍̃̃̉̆̋̋ͥP̠̝̱̿͛ͬͩ̍̅̔ͣr̻̪̤͚̘̰̤͑̿̈̄̍ͯo̫͈̪̭̥͙͛̃̔̀g̔͗ͦ̅ ̝̯̘̣̘͗͆̄̋r̲͓̭͓̪̋ͩͤ͛̑́̎͋a͇̰̼͚̜̅́͌͗̆̅̏ͪͦm̯̤̱̥͇͋͒̈̅̓ͮ ̱̣̞m̖̼̰̟̗̮̬͓͗͋̏̓ͫ̑ͪͅḙ̄ͯͧ̋̋̑̊͗ͅr͕͇ͪ͒̆͗͆̓̀

  5. #5
    Join Date
    Feb 2009
    Location
    Stord, Norway
    Posts
    882

    Default Re: I wish we had static classes in DataFlex

    Sean,
    I see that we think along the same lines.
    Ola Eldøy (Twitter: @olaeld) - Developer at Emma EDB AS

  6. #6
    Join Date
    Feb 2009
    Location
    Stuart, FL
    Posts
    5,321

    Default Re: I wish we had static classes in DataFlex

    we use them a lot and also in DF simply create a class and a global object

    the simplest form

    Code:
    Class cMyClass is a ...
    End_Class
    
    Object oGlobalMyClass is a cMyClass
    End_Object
    then you can either call the object by name or if you want you can use a global integer as well
    Michael Salzlechner
    StarZen Technologies, Inc
    http.://www.starzen.com

    IT Director at Balloons Everywhere

    Development Blog
    http://www.salzlechner.com/dev

    DataFlex Package Manager (aka Nuget for DataFlex)
    http://windowsdeveloper.com/dfPackage

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •