[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

how to get the record type of a pointer?

Posted on 2004-11-02
15
Medium Priority
?
266 Views
Last Modified: 2010-04-05
I have a grid and each cell i assigned a pointer record type. I want to get the pointer record type
assigned to a cell being clicked. Anybody here knows how to get it?
0
Comment
Question by:edeaux
[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
  • 5
  • 2
  • 2
  • +2
15 Comments
 
LVL 7

Expert Comment

by:sftweng
ID: 12470983
What do you mean by "pointer record type". Are they pointers to TObjects? If so you can use the "is" and "as" operators and the TClass meta-classing type to determine the class of the object pointed to by the pointer.

So, for example, if you had a TList containing various pointers to components, you could do things like:

  if myList.Items[myList.ItemIndex] is TEdit
  then with myList.Items[myList.ItemIndex] as TEdit do
  begin
    {do something for the TEdit}
  end;

Be careful with pointers - they are the single biggest problem area in all software - always use strongly typed pointers and use the "is" and "as" operators in preference to coerced typecasting (e.g., TEdit(myList.Items[myList.ItemIndex]).
0
 

Author Comment

by:edeaux
ID: 12471030
If i'm going to use a class instead of a record i can get it's class type by TObject.ClassType. I'm using pointer of records. I want to get the record type of the record pointer assigned to a cell being clicked.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12471258
>I'm using pointer of records.
>I want to get the record type of the record pointer assigned to a cell being clicked.

you have to care to know what recordtypepointer you have.
there is no chance to retrieve the recordtype

meikl ;-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Expert Comment

by:sftweng
ID: 12471629
If you're dealing with records rather than objects, you should probably use a strongly-typed pointer which points to a case-variant record. The variant could be defined by an enumerated list describing which of the possible records is contained within the case-variiant part. Thus the pointer itself will always be of the same type but the record to which it points will define it's own type.
0
 

Author Comment

by:edeaux
ID: 12471666
can you give an example?
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 12471742
type
  TMyVariantRecord = record
      case integer of
         1 : begin
                 Field1 : Boolean;
                 Field2 : Integer;
                 Field3 : String[100];
              end;
         2 : begin
                 Field1 : String[100];
                 Field2 : Integer;
                 Field3 : Boolean;
              end;
         3 : begin
                 Field1 : Integer;
                 Field2 : Boolean;
              end;
   end;
   PMyVariantRecord = ^TMyVariantRecord;

just from head

meikl ;-)
0
 
LVL 7

Assisted Solution

by:sftweng
sftweng earned 100 total points
ID: 12471872
It would be a good idea to create an enumerated list of record types:

 type
  TMyTypes = (rt1,rt2,rt3);

  TMyVariantRecord = record
      case myType : TMyTypes of
         rt1 : begin
                   Field1 : Boolean;
                   Field2 : Integer;
                   Field3 : String[100];
                end;
         rt2 : begin
                   Field1 : String[100];
                   Field2 : Integer;
                   Field3 : Boolean;
                end;
         rt3 : begin
                   Field1 : Integer;
                   Field2 : Boolean;
                end;
   end;
   PMyVariantRecord = ^TMyVariantRecord;
0
 
LVL 34

Assisted Solution

by:Slick812
Slick812 earned 100 total points
ID: 12480116
a way that I have used for using Different sets of Data (records or other) in the same pointers or Record types is to place an Identifier ( ID ) as the first 4 bytes (Integer) of all the record (or data) types so I can  first read the pointers as a PInt and see what type of record it is and then fish out whatever record or data element I need. This is a little more versital and allows you to do many different types. .


type
  PTopRec = ^TTopRec;
  TTopRec = record
//FIRST element of ALL records mut be the same, an ID or an Integer (or byte or word)
    ID, Top, Size, Offset: Integer;
    end;

  PBoundsRec = ^TBoundsRec;
  TBoundsRec = record
    ID, Sys: Integer;
    Bounds: TRect;
    end;

  POtherRec = ^TOtherRec;
  TOtherRec = record
    ID, Other: Integer;
    Pos, RePos: TPoint;
    end;


implementation

{$R *.DFM}

var
Top1: TTopRec;
Bounds1: TBoundsRec;
Other1: TOtherRec;


  // you will need to Initialize your records with the correct ID
procedure InitRec;
begin
Top1.ID := 1;
Top1.Top := 367;

Bounds1.ID := 2;
Bounds1.Sys := 8145;
Bounds1.Bounds := Rect(50,100,150,200);

Other1.ID := 3;
Other1.Other := 12345;
Other1.Pos := Point(10,50);
end;


  // example how to use ID
procedure giveData(pData: Pointer);
begin
if pData = nil then Exit;
if PInt(pData)^ = 0 then
  ShowMessage('Data has not been initialized')
  else
  if PInt(pData)^ = 1 then
  ShowMessage('this is TOP top '+IntToStr(PTopRec(pData)^.Top))
  else
  if PInt(pData)^ = 2 then
  ShowMessage('this is Bounds bounds top '+IntToStr(PBoundsRec(pData)^.Bounds.Top))
  else
  if PInt(pData)^ = 3 then
  ShowMessage('this is Other Pos X '+IntToStr(POtherRec(pData)^.Pos.x));
end;


// button click for use procedure
procedure TForm1.but_RecPointerClick(Sender: TObject);
begin
InitRec;
giveData(@Other1);
giveData(@Bounds1);
end;


the variants use a simular method, they have a type ID in them. . .
0
 
LVL 7

Expert Comment

by:sftweng
ID: 12480910
Slick812, the record case variant accomplishes the same thing, in a way "blessed" by the language and enforcing strong typing.
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 100 total points
ID: 12481475
There is NO way to recognize the record type of the thing a pointer is pointing to, unless you yourself maintain some kind of information somewhere of the recordtype yourself.

With classes it is simple. The class pointer points to a record structure of the class itself and this record contains a pointer to the class definition somewhere else. (The Virtual Method Table.) Delphi stores quite a lot of data about classes. But Delphi stores absolutely NO information about records by itself. It does have information about recordtypes in the RTTI somewhere but then you have to know the type. But when you put a record in memory, Delphi just stores the contents of the record in that location and nothing else. No type information, nothing...
0
 
LVL 34

Expert Comment

by:Slick812
ID: 12486375
sftweng , , of course, I did not mean to indicate or imply that they did not, sorry if I worded my comment to give that Idea. . . the original question specficaly asked about pointer record,  but thats so general, I may have mis-understood the question
0
 
LVL 7

Expert Comment

by:sftweng
ID: 13345469
I recommend a split among the responders
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

656 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