Results 1 to 8 of 8

Thread: Properties

  1. #1

    Properties

    Charles,
    are there any plans to add properties (to classes) like in FreeBasic or FreePascal?

    Thanks
    efgee

  2. #2

    Re: Properties

    Hi efgee,

    This is the easiest O2 way to express classes with properties and methods:

    [code=thinbasic]
    '
    '---------------------------------
    '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


    [/code]

    but you can also define methods outside the class declaration:

    [code=thinbasic]
    '
    '---------------------------------
    '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


    [/code]

    Charles

  3. #3

    Re: Properties

    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

    Bye
    efgee

  4. #4

    Re: Properties


    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?

    [code=thinbasic]
    brick br
    br.SetMaterial="concrete"
    br.SetSize=20,10,7.5
    'or
    br.SetSize=(20,10,7.5)

    print "Brick weight: " br.weight()

    [/code]

    Charles

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    Re: Properties

    I think efgee wants what is called property or accessor in other languages.
    Oxygen already has it, although it is defined using method syntax:
    [code=thinbasic]

    '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
    [/code]


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6

    Re: Properties


    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

  7. #7

    Re: Properties

    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.
    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.



    Thank you both.
    efgee

  8. #8
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405

    Re: Properties

    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!!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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