Author Topic: setters & getters  (Read 1434 times)

0 Members and 1 Guest are viewing this topic.

jcfuller

  • Guest
setters & getters
« on: February 23, 2018, 08:50:39 AM »
While testing in the oop folder I noticed Charles using setters and getters for class variables. I originally used a c++ template for them but decided to go with this method which seems to work fine with OxygenBasic also.
I have always prefixed all private class variables with m_ .
James
Code: [Select]
use rtl64
class testgs
    private
        sys m_one,m_two
        string m_s1
    public
        Method one(sys a)
            m_one = a
        End Method
        Method one() As sys
            method = m_one
        End Method     
       
        Method two(sys a)
            m_two = a
        End Method
        Method two() As sys
            method = m_two
        End Method
       
        Method s1(string s)
            m_s1 = s
        End Method
        Method s1() As string
            method = m_s1
        End Method
end class

dim test As testgs
test.one 11        'test.one(11)
test.two 22        'test.two(22)
test.s1 "james"    'test.s1("james")

print test.one    'test.one()
print test.two    'test.two()
print test.s1    'test.s1()


jcfuller

  • Guest
Re: setters & getters
« Reply #1 on: February 23, 2018, 12:34:53 PM »
Well I should have done one more example before posting this.
PseudoVariables.o2bas does the same thing :)

James

José Roca

  • Guest
Re: setters & getters
« Reply #2 on: February 23, 2018, 01:08:03 PM »
The OOPers would like test.one = 11 instead of test.one 11.

Charles Pegge

  • Guest
Re: setters & getters
« Reply #3 on: February 23, 2018, 05:53:53 PM »
pseudo-assign notation can be used with any procedure, including the mid statement.

The compiler checks for '=' after a closing bracket

Code: [Select]
string s="abcd"
mid(s,2,"B")
mid(s,3)="C"
print s 'aBCd

Charles Pegge

  • Guest
Re: setters & getters
« Reply #4 on: February 23, 2018, 08:19:27 PM »
I found a bug when using pseudo-assign with 1 param passed byref. I will include the fix in the next update.

Getters and Setters can be expressed in short form if so desired:

Code: [Select]
class GetSetter
  int    v_1
  string v_2
  int    v1()         {return v_1}
  sub    v1(int v)    {v_1=v}
  string v2()         {return v_2}
  sub    v2(string v) {v_2=v}
end class
'#recordof getSetter
'TEST
GetSetter g
g.v1=42
print g.v1 '42'