Board index » delphi » type casting (objects)

type casting (objects)

Hm, what do you usually do if you want to type-cast, say, tview pointer as
tdialog one?
I mean, tgroup.current returns pointer to active view (be it a window,
dialog, whatever), which is declared as pview.
How do I execute some method of tdialog, if active window is a dialog, and,
therefore, pointer returned by tgroup.current points to
an instance of tdialog object?
I'm searching for a civilized way to do so:)) Would be very thankful for one
who points thist way out.
 

Re:type casting (objects)


Quote
In article <b3gf5i$60...@hyppo.gu.net>, RG RG wrote:
> Hm, what do you usually do if you want to type-cast, say, tview pointer as
> tdialog one?

tdialog(tview)

Quote
> I mean, tgroup.current returns pointer to active view (be it a window,
> dialog, whatever), which is declared as pview.
> How do I execute some method of tdialog, if active window is a dialog, and,
> therefore, pointer returned by tgroup.current points to
> an instance of tdialog object?

pdialog(tmygroup.current)^.method(param1,param2);

Quote
> I'm searching for a civilized way to do so:)) Would be very thankful for one
> who points thist way out.

There is some danger in this, because it doesn't check if it is a dialog. So if somehow
you cast the wrong pointer, you probably get a nice RTE 216.

But afaik there is no systematic way to check this in BP/TP. (Delphi has IS
and AS)

Re:type casting (objects)


  Can't you derive your own objects in turbo vision ? I you can then one could
create an object (TMyDialog ) from a TDialog object and add ones own IS method.
Quote

>In article <b3gf5i$60...@hyppo.gu.net>, RG RG wrote:
>> Hm, what do you usually do if you want to type-cast, say, tview pointer as
>> tdialog one?

>tdialog(tview)

>> I mean, tgroup.current returns pointer to active view (be it a window,
>> dialog, whatever), which is declared as pview.
>> How do I execute some method of tdialog, if active window is a dialog, and,
>> therefore, pointer returned by tgroup.current points to
>> an instance of tdialog object?

>pdialog(tmygroup.current)^.method(param1,param2);

>> I'm searching for a civilized way to do so:)) Would be very thankful for
>one
>> who points thist way out.

>There is some danger in this, because it doesn't check if it is a dialog. So
>if somehow
>you cast the wrong pointer, you probably get a nice RTE 216.

>But afaik there is no systematic way to check this in BP/TP. (Delphi has IS
>and AS)

Re:type casting (objects)


"Marco van de Voort" <mar...@toad.stack.nl> schreef in bericht
news:slrnb5nkmu.1cif.marcov@toad.stack.nl...

Quote
> In article <b3gf5i$60...@hyppo.gu.net>, RG RG wrote:
> > Hm, what do you usually do if you want to type-cast, say, tview
pointer as
> > tdialog one?

In fact you are telling the compiler not to check the type of parameter
or object at a given location, but to consider it as the type specified
by your typecasting.

Quote
> tdialog(tview)

> > I mean, tgroup.current returns pointer to active view (be it a
window,
> > dialog, whatever), which is declared as pview.
> > How do I execute some method of tdialog, if active window is a
dialog, and,
> > therefore, pointer returned by tgroup.current points to
> > an instance of tdialog object?

> pdialog(tmygroup.current)^.method(param1,param2);

> > I'm searching for a civilized way to do so:)) Would be very thankful
for one
> > who points thist way out.

> There is some danger in this, because it doesn't check if it is a

dialog. So if somehow

Quote
> you cast the wrong pointer, you probably get a nice RTE 216.

> But afaik there is no systematic way to check this in BP/TP. (Delphi
has IS
> and AS)

Yes in BP/TP there is a possibility to check the type of an object by
means of the typeof function.
Something like
if typeof (tgroup)=typeof(tmyobject) then.....

I use this for determining the type in a tcollection filled with
different types of objects

--
Femme

Re:type casting (objects)


There seems to be another way to check object types - pointers to
constructor init are all equal for every instance of an object.
Replies were very helpful, thank you.

Quote
"Femme Verbeek" <fv[at]{*word*104}jet[dot]nl> wrote in message

news:v5o64rkjo9tg11@corp.supernews.com...
Quote

> "Marco van de Voort" <mar...@toad.stack.nl> schreef in bericht
> news:slrnb5nkmu.1cif.marcov@toad.stack.nl...
> > In article <b3gf5i$60...@hyppo.gu.net>, RG RG wrote:
> > > Hm, what do you usually do if you want to type-cast, say, tview
> pointer as
> > > tdialog one?

> In fact you are telling the compiler not to check the type of parameter
> or object at a given location, but to consider it as the type specified
> by your typecasting.

> > tdialog(tview)

> > > I mean, tgroup.current returns pointer to active view (be it a
> window,
> > > dialog, whatever), which is declared as pview.
> > > How do I execute some method of tdialog, if active window is a
> dialog, and,
> > > therefore, pointer returned by tgroup.current points to
> > > an instance of tdialog object?

> > pdialog(tmygroup.current)^.method(param1,param2);

> > > I'm searching for a civilized way to do so:)) Would be very thankful
> for one
> > > who points thist way out.

> > There is some danger in this, because it doesn't check if it is a
> dialog. So if somehow
> > you cast the wrong pointer, you probably get a nice RTE 216.

> > But afaik there is no systematic way to check this in BP/TP. (Delphi
> has IS
> > and AS)

> Yes in BP/TP there is a possibility to check the type of an object by
> means of the typeof function.
> Something like
> if typeof (tgroup)=typeof(tmyobject) then.....

> I use this for determining the type in a tcollection filled with
> different types of objects

> --
> Femme

Re:type casting (objects)


"RG RG" <ragnar0ck_FORSPAMME...@ukr.net> schreef in bericht
news:b3ip7l$lao$1@hyppo.gu.net...

Quote
> There seems to be another way to check object types - pointers to
> constructor init are all equal for every instance of an object.
> Replies were very helpful, thank you.

Are you sure this will give a unique result for every object type? What
if the constructor is an inherited method?
The typeof function gives you a pointer to the objects vitual method
table.
Other preferable method is to start with a mother type of object.
Give it a virtual method of the common operation you want to do.
All the objects that you work with, decend from the mother type, where
every type defines its own unique virtual method for the operation you
wanted to do.

--
Femme

Re:type casting (objects)


Quote
> Are you sure this will give a unique result for every object type? What
> if the constructor is an inherited method?

Well, the book says so, and I've checked this manually.
Anyhow, mechanism for that and typeof() seems to be identical.

Quote
"Femme Verbeek" <fv[at]{*word*104}jet[dot]nl> wrote in message

news:v5qelk50b4sj22@corp.supernews.com...
Quote

> "RG RG" <ragnar0ck_FORSPAMME...@ukr.net> schreef in bericht
> news:b3ip7l$lao$1@hyppo.gu.net...
> > There seems to be another way to check object types - pointers to
> > constructor init are all equal for every instance of an object.
> > Replies were very helpful, thank you.

> The typeof function gives you a pointer to the objects vitual method
> table.
> Other preferable method is to start with a mother type of object.
> Give it a virtual method of the common operation you want to do.
> All the objects that you work with, decend from the mother type, where
> every type defines its own unique virtual method for the operation you
> wanted to do.

> --
> Femme

Other Threads