Typecasting in OOP 
Author Message
 Typecasting in OOP

A while back some may recall that I posted a message asking about
OOP typecasting.  Thanks to all who responded.  Now, though, I have
another dilema!  It's a similar problem, but not quite the same.
Supposing I have the following declared:

PGroup = ^TGroup;
TGroup = OBJECT
    Parent : PWindow;
    Button : PButton;
    Procedure InitButton;
End;

PNewButton = ^TNewButton;
TNewButton = OBJECT (TButton)
    NewField : Integer;
End;

I would declare TGroup.Button like this:

    Procedure TGroup.InitButton; begin
        Button:=New(PNewButton, Init);
    End;

But -- and here's my problem -- I am not allowed to make any reference
to Button^.NewField.  I can see why this is so--because TGroup.Button
was originally declared as a PButton type, not a PNewButton type, so
the compiler will not know that I am pointing to a PNewButton until I
compile.

How can this problem be solved?  It's really hindering my efforts with
a program I'm writing.

Any help appreciated.  Thanks!


--
      ______________
    /               \
   |   Sam McGrath   |     This is a temporary SIG...it's crappy, I know
    \_______________/



Wed, 18 Jun 1902 08:00:00 GMT  
 Typecasting in OOP
A while back some may recall that I posted a message asking about
OOP typecasting.  Thanks to all who responded.  Now, though, I have
another dilema!  It's a similar problem, but not quite the same.
Supposing I have the following declared:

PGroup = ^TGroup;
TGroup = OBJECT
    Parent : PWindow;
    Button : PButton;
    Procedure InitButton;
End;

PNewButton = ^TNewButton;
TNewButton = OBJECT (TButton)
    NewField : Integer;
End;

I would declare TGroup.Button like this:

    Procedure TGroup.InitButton; begin
        Button:=New(PNewButton, Init);
    End;

But -- and here's my problem -- I am not allowed to make any reference
to Button^.NewField.  I can see why this is so--because TGroup.Button
was originally declared as a PButton type, not a PNewButton type, so
the compiler will not know that I am pointing to a PNewButton until I
compile.

How can this problem be solved?  It's really hindering my efforts with
a program I'm writing.

Any help appreciated.  Thanks!




Wed, 18 Jun 1902 08:00:00 GMT  
 Typecasting in OOP

Quote:

>A while back some may recall that I posted a message asking about
>OOP typecasting.  Thanks to all who responded.  Now, though, I have
>another dilema!  It's a similar problem, but not quite the same.
>Supposing I have the following declared:
>PGroup = ^TGroup;
>TGroup = OBJECT
>    Parent : PWindow;
>    Button : PButton;
>    Procedure InitButton;
>End;
>PNewButton = ^TNewButton;
>TNewButton = OBJECT (TButton)
>    NewField : Integer;
>End;
>I would declare TGroup.Button like this:
>    Procedure TGroup.InitButton; begin
>        Button:=New(PNewButton, Init);
>    End;
>But -- and here's my problem -- I am not allowed to make any reference
>to Button^.NewField.  I can see why this is so--because TGroup.Button
>was originally declared as a PButton type, not a PNewButton type, so
>the compiler will not know that I am pointing to a PNewButton until I
>compile.
>How can this problem be solved?  It's really hindering my efforts with
>a program I'm writing.
>Any help appreciated.  Thanks!


Instead of
  Button^.NewField := 1;
use:
  PNewButton(Button)^.NewField := 1;
or:
  with PNewButton(Button)^ do begin
    NewField := NewField + 1;
    NewField := NewField * 3;
  end;

---------------------------------------------------------------------

---               Compuserve: 100551,2212                         ---
---------------------------------------------------------------------
---                      WWW: http://www.lrz-muenchen.de/~p7003ot ---
---------------------------------------------------------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 Typecasting in OOP

Quote:


>Subject: Typecasting in OOP
>Date: 25 Nov 1995 17:56:18 -0800
>A while back some may recall that I posted a message asking about
>OOP typecasting.  Thanks to all who responded.  Now, though, I have
>another dilema!  It's a similar problem, but not quite the same.
>Supposing I have the following declared:
>PGroup = ^TGroup;

                                               <--------------
Quote:
>TGroup = OBJECT                            | {put the statement}
>    Parent : PWindow;                        | {here}
>    Button : PButton;                          |
>    Procedure InitButton;                 |
>End;                                                       |

                                                                  |

Quote:
>PNewButton = ^TNewButton; -----
>TNewButton = OBJECT (TButton)
>    NewField : Integer;
>End;
>I would declare TGroup.Button like this:
>    Procedure TGroup.InitButton; begin
>        Button:=New(PNewButton, Init);
>    End;

The solution is quite simple: place the PNewButton = ^TNewButton;
statement before the definition of the TGroup object (this is allowed
by the compiler). Now you can define the Button field in the TGroup
object as a PNewButton type and your problem is solved.
In the source i've marked the place where to put the PNewButton = ..
statement.

Good luck,

Harold Veelenturf

the Netherlands



Wed, 18 Jun 1902 08:00:00 GMT  
 Typecasting in OOP

[...]

Quote:
>Supposing I have the following declared:

>PGroup = ^TGroup;
>TGroup = OBJECT
>    Parent : PWindow;
>    Button : PButton;
>    Procedure InitButton;
>End;

>PNewButton = ^TNewButton;
>TNewButton = OBJECT (TButton)
>    NewField : Integer;
>End;

>I would declare TGroup.Button like this:

>    Procedure TGroup.InitButton; begin
>        Button:=New(PNewButton, Init);
>    End;

>But -- and here's my problem -- I am not allowed to make any reference
>to Button^.NewField.

[...]

Just use normal typecasting, if you're sure that you have a NewButton:

Procedure TGroup.InitButton;
  begin
     Button:=New(PNewButton, Init);
     PNewButton(Button)^.NewField := 42;
  end;

If Button is not really a PNewButton, you get some real problems,
though.  In Delphi you can test the type of Button, in BPW, you
can do a little but not enough of testing...
Hope this helps,
Jochen

________________________________________________________________________

I post, therefore I am.



Wed, 18 Jun 1902 08:00:00 GMT  
 Typecasting in OOP

Quote:

>A while back some may recall that I posted a message asking about
>OOP typecasting.  Thanks to all who responded.  Now, though, I have
>another dilema!  It's a similar problem, but not quite the same.
>Supposing I have the following declared:

>PGroup = ^TGroup;
>TGroup = OBJECT
>    Parent : PWindow;
>    Button : PButton;
>    Procedure InitButton;
>End;

>PNewButton = ^TNewButton;
>TNewButton = OBJECT (TButton)
>    NewField : Integer;
>End;

>I would declare TGroup.Button like this:

>    Procedure TGroup.InitButton; begin
>        Button:=New(PNewButton, Init);
>    End;

>But -- and here's my problem -- I am not allowed to make any reference
>to Button^.NewField.  I can see why this is so--because TGroup.Button
>was originally declared as a PButton type, not a PNewButton type, so
>the compiler will not know that I am pointing to a PNewButton until I
>compile.

You should refer to pNewButton (Button)^.NewField
Since Button actually IS a pNewButton this is OK.

--
Jos van Kan           http://dutita0.twi.tudelft.nl/users/vankan
Math Dept, Delft Univ of Technology, Delft Netherlands.



Wed, 18 Jun 1902 08:00:00 GMT  
 Typecasting in OOP

Quote:

>PGroup = ^TGroup;
>TGroup = OBJECT
>    Parent : PWindow;
>    Button : PButton;
>    Procedure InitButton;
>End;

>PNewButton = ^TNewButton;
>TNewButton = OBJECT (TButton)
>    NewField : Integer;
>End;

>I would declare TGroup.Button like this:

>    Procedure TGroup.InitButton; begin
>        Button:=New(PNewButton, Init);
>    End;

>But -- and here's my problem -- I am not allowed to make any reference
>to Button^.NewField.  I can see why this is so--because TGroup.Button
>was originally declared as a PButton type, not a PNewButton type, so
>the compiler will not know that I am pointing to a PNewButton until I
>compile.

Button^ does not HAVE a NewField. If you need to refer to a NewField, you
need to be using a TNewButton; and it is TNewButton which should contain
an Init which uses NewField.

If you have existing TPUs which refer to "Button", and you want to add the
new property to this type, I think you can & need to arrange that a new
Unit is needed in the inheritance chain of the type (this may need
"inherited" and hence TP7/BP7, not TP6).

IMHO, the typecastings of pointers recommended by other posters are a BODGE,
which may well work in a particular instance but is fundamentally unsound.

BTW, you might do better to ask in the TurboVision group, since although the
above is not necessarily TV-related, handling objects in this way is a very
TV-ish thing to do.

--
Dr John Stockton, Surrey, UK



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Typecast SET to INTEGER ?

2. Invalid Class Typecast

3. SQL-Typecasting to boolean ?

4. value typecasts

5. A question about typecasting

6. typecasting preferences

7. Interesting TypeCasting Problem

8. Delphi newbie: Integer to extended typecast

9. 'Money wanted' algorithm (OOP version)

10. Is Borland Pascal full OOP compliant?

11. OOP and DB programming

12. New Book: From COBOL to OOP

 

 
Powered by phpBB® Forum Software