Oxygen Basic
Programming => Example Code => General => Topic started by: jcfuller 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
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()
-
Well I should have done one more example before posting this.
PseudoVariables.o2bas does the same thing :)
James
-
The OOPers would like test.one = 11 instead of test.one 11.
-
pseudo-assign notation can be used with any procedure, including the mid statement.
The compiler checks for '=' after a closing bracket
string s="abcd"
mid(s,2,"B")
mid(s,3)="C"
print s 'aBCd
-
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:
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'