Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic Examples => Topic started by: Frank Brübach on November 14, 2024, 05:29:26 PM

Title: Class statics
Post by: Frank Brübach on November 14, 2024, 05:29:26 PM
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
Title: Re: Class statics
Post by: Charles Pegge on November 14, 2024, 10:19:09 PM
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

Title: Re: Class statics
Post by: Frank Brübach on November 15, 2024, 07:20:42 PM
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