Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Need create and call method from many class types (https://www.delphipraxis.net/157295-need-create-call-method-many-class-types.html)

WojTec 6. Jan 2011 19:26

Delphi-Version: 2010

Need create and call method from many class types
 
Hello, guys,

So, lets say what I have.

I have two classes:
- TClass1(TBaseClass)
- TClass2(BaseClass)

TClass1 and TClass2 extands TBaseClass, both are for different input data types. TClass1/TClass2 has LoadFromStream and SaveToStream.

Now, how to create and call methods with names that are common for any class TSomeOtherClass(TClass1/TClass2), like stream procedures?

Currently I have:

if class = someclass then someclass.loadfromstream
else if class = otherclass then otherclass.loadfromstream
else if class = yetanotherclass then yetanotherclass.loadfromstream

but in future I'll have too many classes to add if statement in few places in few units in program.

Could you help with this?

mkinzler 6. Jan 2011 19:32

AW: Need create and call method from many class types
 
Why not
Delphi-Quellcode:
if class is TBaseclass then BaseClass( class).loadfromstream());
?

WojTec 6. Jan 2011 19:51

Re: Need create and call method from many class types
 
And this will call correct method (someclass.loadfromstream)?

mkinzler 6. Jan 2011 19:56

AW: Need create and call method from many class types
 
Yes, if BaseClass is a superclass of class

WojTec 6. Jan 2011 20:15

Re: Need create and call method from many class types
 
What it mean? This is somethink like this (I saw somewhere, but don't know how it working):

Delphi-Quellcode:
TClassOfBase = class of TBaseClass


Delphi-Quellcode:
TBaseClass = class;
TSomeClassA = class(TBaseClass);
TSomeClassB = class(TBaseClass);

//(TSomeClassA and TSomeClassB) are base for any class I'm working with it).

I have TClass1(TSomeClassA), TClass2(TSomeClassB), etc. How it should be done to avoid many thousends if-then-else?

Could you explain me, because I don't understand?

mkinzler 6. Jan 2011 20:29

AW: Need create and call method from many class types
 
Because of the IS-A paradigm each Subclass "is a" Baseclass

if the wanted function/procedure is declared in a superclass ( baseclass->MiddleClass->Class1) it can be called as function/procedure of the superclass. It'S always called the "best matched" function.
if the function is implemented in the Baaseclass this function is called, if the implementation is in Class1/Class2 then the corresponding versions are used:
Delphi-Quellcode:
TBaseClass(class1).AnyFunc()
calls Tclass1.AnyFunc()

Aphton 7. Jan 2011 08:54

AW: Need create and call method from many class types
 
If you want to figure out how the "Class of Class" thing works, just try to understand the TPicture.LoadFromFile method, which can be found in the "graphics" unit.

Stevie 10. Jan 2011 11:52

AW: Need create and call method from many class types
 
TBaseClass has to have LoadFromStream as virtual (abstract).


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:55 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz