Solved

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

Posted on 2004-03-22
3
299 Views
Last Modified: 2010-04-05
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;
 end;

Instead i would want
var
  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.

Chad
0
Comment
Question by:Chadak
  • 2
3 Comments
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility

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.

eg

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

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
begin
  result:=TShape(MyShape).Suface;
end;

------

Hope this helps,
Russell




0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
Comment Utility
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:
type
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 ;-)

0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
Comment Utility

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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now