Board index » delphi » Get Object-Reference

Get Object-Reference

I don't know if I got a blackout, but I guess this problem is either
too simple or too hard. Anyway:

I have two objects Example1,Example2 of two different classes
TExample1 and
TExample2 with two different Interfaces

TExample1 = class(TInterfacedObject,I1)
public
  data:integer;
  feature:TFeature;
end;

TExample1 = class(TInterfacedObject,I2)
public
  data:integer;
  feature:TFeature;
end;

Both classes use the TFeature-class:

TFeature = class
procedure showme;
end;

var Example1:TExample1;
    Example2:TExample2;

While working with the objects I want to know which Interface is
implemented from my used objects while I'm in the showme-procedure.
So, I basically want to find out if the function showme is called from
Example1 or Example2. I want to know that, because:
1. I want to react differently on different interfaces and
2. want to use the data-variable of the example-objects:

procedure Tfeature.showme;
var in1:i1;
    in2:i2;
begin
 if supports(getobject,I1,in1) then
  getobject.data:=1;
 if supports(getobject,I2,in2) then
  getobject.data:=2;
end;

"Getobject" should be something to reference to the object the
tfeature-class is created in, being example1 or example2.

Any ideas?

Jrgen

 

Re:Get Object-Reference


"Juergen Schlinker" <juergenschlin...@gmx.de> skrev i melding
news:ab8f5bd6.0211251609.79aa9a7b@posting.google.com...
[...]

Quote
> While working with the objects I want to know which Interface is
> implemented from my used objects while I'm in the showme-procedure.
> So, I basically want to find out if the function showme is called from
> Example1 or Example2. I want to know that, because:
> 1. I want to react differently on different interfaces and
> 2. want to use the data-variable of the example-objects:

> procedure Tfeature.showme;
> var in1:i1;
>     in2:i2;
> begin
>  if supports(getobject,I1,in1) then
>   getobject.data:=1;
>  if supports(getobject,I2,in2) then
>   getobject.data:=2;
> end;

> "Getobject" should be something to reference to the object the
> tfeature-class is created in, being example1 or example2.

> Any ideas?

Have a look at TObject.GetInterface method..

--
Bj?rge S?ther
bjorge@hahaha_itte.no

Re:Get Object-Reference


Hi,

You could provide a TObject pointer p to the parent-object (which has
type TExample1 or TExample2) and test the parent-type in this way:

if p is TExample1 then ...
if p is TExample2 then ...

Regards,

Martin.

Re:Get Object-Reference


Im Artikel <ab8f5bd6.0211251609.79aa9...@posting.google.com>,
juergenschlin...@gmx.de (Juergen Schlinker) schreibt:

Quote
>"Getobject" should be something to reference to the object the
>tfeature-class is created in, being example1 or example2.

Then you must store that information inside the TFeature object (class). The
type of that information must be choosen in an appropriate way, so that all
required information can be retrieved from that data (object/interface) type.
You can use the TObject or TInterfacedObject type for that back-reference, and
test with:
if MyContainer is TExample1 then
  TExample1(MyContainer).data := 1
else if MyContainer is TExample2 then
...

It may be simpler to define an IContainer interface, which exposes the data
property or whatever is needed, and construct:
TExample1 = class(TInterfacedObject, IContainer, I1)
...

TFeature = class
  MyContainer: IContainer;
...

Even easier, but perhaps not illustrative for the use of interfaces, you can
construct a base class:

TFeaturedExample = class(TInterfacedObject)
  procedure showme; virtual; abstract;
...

and override the showme procedure in the derived classes:

TExample1 = class(TFeaturedExample, I1)
  procedure showme; override;
...

Then you also can make the showme procedure part of I1 and I2, if that helps.
What you tried to implement is a showyou procedure, which is difficult to
implement from outside a class. What should showyou do, when it is given an
object of an unknown and consequently unhandled type?

[It should call showme of the given interface or object ;-]

DoDi

Other Threads