Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic Examples => Topic started by: Frank Brübach on February 26, 2025, 09:28:33 PM

Title: Class example findme
Post by: Frank Brübach on February 26, 2025, 09:28:33 PM

' simple class example "findme"
' oxygen
'
class AvengersBank
  '
  string buffer
  '
  method store(string skey,text)
    buffer += chr(1) + skey + chr(2) + text + chr(3) + chr(13)

  end method
  '
  method findme(string skey) as string
    integer a,b,c
    a=instr 1,buffer,chr(1) + skey + chr(2)
    if a then
      b=instr a, buffer, chr(2)
      c=instr a, buffer, chr(3)
      return mid buffer, b+1, c-b-1
    end if
    return "( "+skey+ " not found )"
  end method
  '
  method clear()
    buffer = ""
  end method
  '
end class

AvengersBank bat
bat.store "ironman","technological armament"
bat.store "thor","hammer"
bat.store "dr strange","wizzard"
bat.store "captain america","shield"
bat.store "spiderman","net power"

string st = bat.findme("spiderman")
print st
Title: Re: Class example findme
Post by: Charles Pegge on February 26, 2025, 10:42:02 PM
Thanks Frank,
You might also find this useful. It saves destroying the old buffer every time you add a new word.

'STRING STORAGE BUFFER
'for printing in one go
'and other uses
'
string prbuf 'string buffer
string prw   'expression to store 
int    pri   'index in prbuf
int    lw    'length of prw
'
def printa
  prw=%1 'expression to store
  lw=len(prw)
  while len(prbuf) < pri+lw
    prbuf+=nuls 1024  'extend prbuf size
  wend                '
  mid prbuf,pri+1,prw 'patch in prw
  pri+=lw             'shift the index
end def

'TEST:
'=====
printa("helo"+" ")
printa("world")
printa("!")
print left prbuf, pri
'
'
'CLEARING THE UFFER TO USE AGAIN
pri=0    'reset the index
prbuf="" 'necessary when using instr() to search.
Title: Re: Class example findme
Post by: Frank Brübach on March 01, 2025, 09:52:17 PM
Another interesting class example:
Thanks Charles for your Last example above.

'
' Classes: The methods are defined inside the class block
'
'---------- //
' 1. class
'-------------
class cuboid
'=============

protected

height as double
width as double
length as double

public

method surface() as double
return (length*width+length*height + width*height)*2
end method

method volume() as double
return length*width*height
end method

method setsize(l as double,w as double, h as double)
length = l : width = w : height = h
end method

end class

cuboid cu
cu.setsize 10,5,2

string cr = chr (13,10)
string tab = chr (9)

print "Volume:" tab cu.volume() cr "Surface:" tab cu.surface()
' volume: 100, surface: 160

'---------- //
' 2. class
class brick
'==========

has cuboid 'dimensions and also ..

protected

density as double
material as string

public

method setmaterial(m as string)
material = m
density = 1
if m = "ceramic" then density = 2
if m = "concrete" then density = 2.5
if m = "ice" then density = 0.95
end method

method weight() as double
return volume()*density
end method

end class

brick br
br.SetMaterial "concrete"
br.SetSize 20,10,7.5

print "Brick weight: " br.weight ' 3750

' ends