?
Solved

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

Posted on 2004-03-22
3
Medium Priority
?
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 23

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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 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