Solved

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

Posted on 2004-03-22
3
300 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
ID: 10651945

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
ID: 10655822
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
ID: 10661852

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

912 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

18 Experts available now in Live!

Get 1:1 Help Now