Class statics

Started by Frank Brübach, November 14, 2024, 05:29:26 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

Hello. Found this class example Yesterday but it didnt Work with static Long aa for example. Only with Long aa its wirking

uses console

'-------------
'CLASS STATICS
'=============

  ' problem: original below doesnt work with 'static' long aa, only with long aa, long bb

  class cls
    'static long aa  ' SHARED CLASS ELEMENT
    long aa
    'static long bb  ' SHARED CLASS ELEMENT
    long bb
    long cc         ' OBJECT ELEMENT
  end class
 
  'dim as cls a,b
  new cls a
  new cls b

  a.bb=42 'member not found a.bb
  a.aa=22
  b.cc=84
  b.aa-=1
  a.cc-=1
  print `
  `     str(b.aa) ` '21
  `     str(b.bb) ` '42
  `     str(b.cc) ` '84
  `
print "ok"
wait

Charles Pegge

Hi Frank,

It must be an old example. Currently, class static variables are only visible inside the class. But they can be accessed from the outside indirectly using methods:

'CLASS STATICS
'=============

uses console

  class ccc
    static long aa  ' SHARED CLASS ELEMENT
    long c          ' OBJECT ELEMENT
    method tally()
      aa++
    end method
    method count() as int
      return aa
    end method
  end class
 
  dim as ccc a,b
  ccc.tally()
  print "Access Class static:" cr
  print "ccc.count" tab ccc.count() cr
  print "a.count  " tab a.count() cr
  print "b.count  " tab b.count() cr
  print "ok"
wait


Frank Brübach

Thank you Charles for your example Works Here :-)

Made a private static Long aa and thats running too

''
'' Re: Class statics2 alternative, frank bruebach
'' private: static long aa runs too :-)
''

' CLASS STATICS
'==============

uses console
   private: '' alternative runs too
   static long aa

  class ccc
    'static long aa  ' SHARED CLASS ELEMENT
    long c          ' OBJECT ELEMENT
    method tally()
      aa++
    end method
    method count() as int
      return aa
    end method
  end class
 
  dim as ccc a,b
  ' new ccc a ' go
  ' new ccc b ' go
  ccc.tally()
  print "Access Class static:" cr
  print "ccc.count" tab ccc.count() cr
  print "a.count  " tab a.count() cr
  print "b.count  " tab b.count() cr
  print "ok"
wait