Trouble with Property returning by propName = value (solved)

General FreeBASIC programming questions.
fxm
Posts: 8756
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Trouble with Property returning by propName = value (solved)

Postby fxm » Jan 19, 2019 20:29

Only one indexing parameter is allowed for a get/set-Property.
Tourist Trap
Posts: 2644
Joined: Jun 02, 2015 16:24

Re: Trouble with Property returning by propName = value (solved)

Postby Tourist Trap » Jan 19, 2019 20:33

fxm wrote:Only one indexing parameter is allowed for a get/set-Property.

For a setter, 2 slots are allowed. Ok I never paid attention to this :)
Does this mean that the getter index is an integer only?
fxm
Posts: 8756
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Trouble with Property returning by propName = value (solved)

Postby fxm » Jan 19, 2019 20:39

No, the indexing parameter can be of any type.

KeyPgProperty → fxm [Added note on returning from an indexed get-Property]
dodicat
Posts: 5614
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Trouble with Property returning by propName = value (solved)

Postby dodicat » Jan 19, 2019 22:49

Could a byref function be more versatile than property?
You can have many parameters, get and set, and the code is more readable and shorter.

Code: Select all

 

#include "crt.bi"
type UDT extends OBJECT
    declare function Prop() byref as integer
    private:
    as integer ref=1
end type

function UDT.Prop() byref as integer
    Prop = ref
end function

dim as UDT uuu
? uuu.Prop



type UDT2 extends OBJECT
    declare function Prop2(byval as integer=0,byref as zstring="",byref as any ptr=0) byref as integer
    private:
    as integer ref=2
end type

function UDT2.Prop2(byval z as integer,byref s as zstring,byref p as any ptr) byref as integer
    prop2= ref
    if len(s) then print s:p=@s[0]
    if z then  system_("pause")
end function

dim as UDT2 vvv
print vvv.Prop2

vvv.prop2=123
print vvv.prop2

dim as udt2 u
print u.prop2

dim as any ptr z
u.prop2(,"Done",z)
print *cast(zstring ptr,z)

u.prop2(1)

 
fxm
Posts: 8756
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Trouble with Property returning by propName = value (solved)

Postby fxm » Jan 20, 2019 7:04

Yes, but except for:
u.prop2(1)=456
which fails when only the first parameter of the function is used.

Right syntaxes for that case:
(u.prop2(1))=456
or:
u.prop2(1)=>456
Tourist Trap
Posts: 2644
Joined: Jun 02, 2015 16:24

Re: Trouble with Property returning by propName = value (solved)

Postby Tourist Trap » Jan 20, 2019 8:53

dodicat wrote:Could a byref function be more versatile than property?
You can have many parameters, get and set, and the code is more readable and shorter.

Code: Select all

 

#include "crt.bi"
type UDT extends OBJECT
    declare function Prop() byref as integer
    private:
    as integer ref=1
end type

function UDT.Prop() byref as integer
    Prop = ref
end function

dim as UDT uuu
? uuu.Prop



type UDT2 extends OBJECT
    declare function Prop2(byval as integer=0,byref as zstring="",byref as any ptr=0) byref as integer
    private:
    as integer ref=2
end type

function UDT2.Prop2(byval z as integer,byref s as zstring,byref p as any ptr) byref as integer
    prop2= ref
    if len(s) then print s:p=@s[0]
    if z then  system_("pause")
end function

dim as UDT2 vvv
print vvv.Prop2

vvv.prop2=123
print vvv.prop2

dim as udt2 u
print u.prop2

dim as any ptr z
u.prop2(,"Done",z)
print *cast(zstring ptr,z)

u.prop2(1)

 

Yes I more and more think that returning byref is almost a good choice by default all the time. It's true for Cast also. As pointed out by fxm, the only disturbing detail is the patenthesis of the assignment. But I will read it as "evaluate me", so I understand that it is made to target the returnec value and I can see a logic to that. I jusy neec to take the reflex to use it mire often.

edit: my keyboard is drunk?

By the way, dodicat, I'm using your pool game fonts in my game contest project. Thanks by advance , it will preserve sensitive eyes :)

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests