Solved

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

Posted on 2004-03-22
3
301 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert GUI app into console app for Win32 Env 5 100
control image tags in a string ? 12 131
Base1 Encode/Decode 3 77
Delphi and Access based Enumeration 9 64
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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