View Full Version : Properties
Charles,
are there any plans to add properties (to classes) like in FreeBasic or FreePascal?
Thanks
efgee
Charles Pegge
02-09-2010, 08:49
Hi efgee,
This is the easiest O2 way to express classes with properties and methods:
'
'---------------------------------
'SIMPLE CLASSES
'=================================
'The methods are defined inside the class block.
'
basic
'-------------
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
cr=chr 13
tab=chr 9
print "Volume:" tab cu.volume() cr "Surface:" tab cu.surface()
'----------
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
but you can also define methods outside the class declaration:
'
'---------------------------------
'SIMPLE CLASSES
'=================================
'The methods are defined inside the class block.
'
basic
'-------------
class cuboid
'=============
public
method surface() as double
method volume() as double
method setsize(l as double,w as double, h as double)
/
protected
height as double
width as double
length as double
end class
methods of cuboid
'================
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 methods
cuboid cu
cu.setsize 10,5,2
cr=chr 13
tab=chr 9
print "Volume:" tab cu.volume() cr "Surface:" tab cu.surface()
'----------
class brick
'==========
public
has cuboid 'dimensions and also ..
method setmaterial(m as string)
method weight() as double
/
protected
density as double
material as string
end class
methods of brick
'===============
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 methods
brick br
br.SetMaterial "concrete"
br.SetSize 20,10,7.5
print "Brick weight: " br.weight
Charles
Thank you but syntax wise it's not treated as properties...
Method -> br.SetMaterial("concrete")
Still a method -> br.SetMaterial "concrete"
Property -> br.SetMaterial = "concrete"
What you showed me is the "BASIC" way of disregarding the parenthesis - not what I hoped for :unguee:
Bye
efgee
Charles Pegge
03-09-2010, 07:42
You can use parentheses for all procedures if you so desire, but in a simple expression they are optional.
I am lazy :)
Oxygen supports pseudo variables, so you can make a method look like a property for assignment purposes. Is this what you wanted?
brick br
br.SetMaterial="concrete"
br.SetSize=20,10,7.5
'or
br.SetSize=(20,10,7.5)
print "Brick weight: " br.weight()
Charles
Petr Schreiber
03-09-2010, 07:56
I think efgee wants what is called property or accessor in other languages.
Oxygen already has it, although it is defined using method syntax:
'CLASSES AND METHODS
'
Uses "oxygen","file"
Dim As String src = "
class myClass
pA as long
' property set
method A(value as long)
pA = value
end method
' property get
method A() as long
method = pA
end method
end class
'====
'TEST
'====
dim o as myClass
o.A = 6
print o.A
terminate
"
O2_BASIC src
'msgbox 0, o2_view "o2h "+src
If Len(O2_ERROR) Then
MsgBox 0, O2_ERROR : Stop
End If
O2_EXEC
Petr
Charles Pegge
03-09-2010, 08:02
Thanks Petr,
Your example also demonstrates method overloading. The get and set methods have the same name but the compiler selects which one to use, by matching their parameters to the call parameters.
Charles
Hi,
Normally a lexer is very rigid - unambiguous - especially C compilers.
They even give a warning because the last line in the code is not empty. :shock:
Because of that I didn't even try to use a property syntax on a method.
Should have known better.
Oxygen is as ambiguous as it can be, I can use different syntax in the same code and it still compiles... and works.
:eusaclap:
Thank you both.
efgee
Petr and Charles, that example is cool. I like the flexibility of being able to overload the function in a nice minimal syntax as o.A = 7 or print o.A
That is really cool.
Using get and set are nice too. It is very nice to have choices!!