Problem of access to a member of a class

General FreeBASIC programming questions.
arsicg
Posts: 7
Joined: Jan 02, 2015 15:54

Problem of access to a member of a class

Postby arsicg » Jun 16, 2015 2:19

hi!
Can someone tell me why I have this error : error 8: Undefined symbol, P_1 in 'uneInstance.P_2.P_1=200'

with this code:

Code: Select all

type Classe_1 extends object
    private:
    _P_1 as integer=100
   
    public:
    declare property P_1(V as integer)
    declare property P_1()as integer
end type
'*******************************************************************************
property Classe_1.P_1(V as integer)
    _P_1 =V
end property
property Classe_1.P_1()as integer
    return _P_1
end property
'*******************************************************************************
type Classe_2 extends object
    private:
    _P_2 as Classe_1
   
    public:
    declare property P_2(V as Classe_1)
    declare property P_2()as Classe_1
end type
'*******************************************************************************

property Classe_2.P_2(V as Classe_1)
    _P_2 =V
end property
property Classe_2.P_2()as Classe_1
    return _P_2
end property
'*******************************************************************************
dim uneInstance as Classe_2
uneInstance.P_2.P_1=200

print uneInstance.P_2.p_1

sleep


Knowing that the compiler accepted the same code when I use Blocks "with" such as:

Code: Select all

...
dim uneInstance as Classe_2
with uneInstance
    with .P_2
        .P_1=200
    end with
end with
...


But the result is false.

This is a bug!
RockTheSchock
Posts: 226
Joined: Mar 12, 2006 16:25

Re: Problem of access to a member of a class

Postby RockTheSchock » Jun 16, 2015 6:38

Well I wrote some tests and played a bit with byref return. It seems without byref there are some allocations of p1 instances when used syntax with braces. . Remove Byref to see what I mean.

Code: Select all

type Classe_1 extends object
Private:
  _P_1 As integer=100
public:
   declare Property P_1(V as integer)
   declare property P_1()As integer
end type
'*******************************************************************************
property Classe_1.P_1(V as integer)

_P_1 =V
Print "P_1=";_p_1,@_p_1,@this
end property
property Classe_1.P_1()As Integer
Print "return P_1 ",@_P_1,@This
return _P_1
end property
'*******************************************************************************
type Classe_2 extends object   
   private:
   _P_2 as Classe_1

   public:
   declare property P_2(V as Classe_1)
   declare property P_2() ByRef as Classe_1 'Byref !!! return by value is standard
end type
'*******************************************************************************

property Classe_2.P_2(V as Classe_1)
   _P_2 =V

   Print "V=",V.P_1,@V
   Print "P_2=",_P_2.P_1,@_p_2

End Property
property Classe_2.P_2() Byref As Classe_1  'Byref !!! return by value is standard
Print "return P_2 ",@_P_2,@This
   Return _P_2
end property
'*******************************************************************************
dim uneInstance as Classe_2
with uneInstance
    with .P_2
        .P_1=200
        Print  .P_1,"Value of p1 should be 200."
    end with
end With

Print
(uneInstance.P_2).P_1=200
Print uneInstance.P_2.p_1,"Value of p1 should be 200."
Print

with uneInstance
    with .P_2
        .P_1=200
        Print  .P_1,"Value of p1 should be 200."
    end with
end With


sleep
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Problem of access to a member of a class

Postby fxm » Jun 16, 2015 11:12

The problem of mandatory parentheses in the expression '(uneInstance.P_2).P_1 = 200' is specific to the use of chained properties where both GET and SET methods are defined for the property 'P_2':
- In expression without parenthese 'uneInstance.P_2.P_1 = 200', I think that the compiler parser interprets the first term 'uneInstance.P_2' as a SET method instead of the GET method (because perhaps it is at left side!).
- If you suppress the SET method 'property Classe_2.P_2 (V as Classe_1)', which is useless in your example, there is no longer any ambiguity for the parser and the expression without parentheses works also.

Then, in order to initialize '_P_1', the GET method 'uneInstance.P_2 ()' must return a class_1 reference and not a copy (obliging to use a byref return),
arsicg
Posts: 7
Joined: Jan 02, 2015 15:54

Re: Problem of access to a member of a class

Postby arsicg » Jun 16, 2015 21:21

Excellent!
Thank you RockTheSchock, fxm
It was very informative.

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest