Is it possible to cast with the content of a variable?

Hi all!
   This is just a question for knowledge, i was wondering( i checked a bit and found nothing on this )  if it's possible in delphi to cast with the content of a variable.

Like i could have a structure like this

 case MyShape of
   Circle : result := TCircle(MyShape).surface;
   Square :  result := TSquare(MyShape).surface;

Instead i would want
  MyObjectType : string // or any other var type

Result := MyObjectType(MyShape).surface);

This is just a dummy exemple to show what i would like to do, in my program i get to a point that i need to cast my object type. but since i have many object possible my case get really huge and i was thinking that if it was possible to cast like this i could save a lots of lines.

Since i know this is not likely possible to do, points will go to the best explanation of why i can't.

Who is Participating?
Russell LibbySoftware Engineer, Advisory Commented:

I can't seem to get the above to work correctly... Delphi pitches a fit unless its able to resolve all references at compile time. So, it would appear that either the big case statement is needed, or a super class reference that contains the method is required.

Russell LibbySoftware Engineer, Advisory Commented:

Sorry Chad,
You can't cast that way. The compiler will determine that you are attempting to cast a string data type as an object class type, when what you are really trying to do is cast the contents of the string to a class identifier type.

When dealing with related objects that expose the same functionality, eg in your case they expose the Surface property, you should be creating a "super" class for these objects to inherit from.


TShape = class(TObject)
  Surface:  ... {surface declaration}

TCircle = class(TShape)
... declaration of TCircle

TSquare = class(TShape)
... declaration of TSquare

This will allow you to perform a simple cast during runtime, eg:

if MyShape is TShape then


Hope this helps,

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Russell is right and i'd work by the same way...
BTW just from head maybe there should be another way...

I don't understend if MyShape in your Example is a variable or a object, or maybe simply a object variable like the componentname...

However you could create a type like this:
TObjectType = (Circle, Square, Rectangle, Ellipse);

And a Var
ClassArray[low(TObjectType)..High(TObjectType)] of Tobject = (TCircle, TSquare, TRectangle, TEllipse);

Then let's say that MySHape have a property ObjectType: TObjectType.

So (MyShape as ClassArray[MyShape.objectType]).Surface....

I repeat: this is just from head, but what about this Russel? Could this be done in your opinion?

F68 ;-)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.