how to get the record type of a pointer?

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?
edeauxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sftwengCommented:
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
edeauxAuthor Commented:
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
kretzschmarCommented:
>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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

sftwengCommented:
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
edeauxAuthor Commented:
can you give an example?
0
kretzschmarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sftwengCommented:
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
Slick812Commented:
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
sftwengCommented:
Slick812, the record case variant accomplishes the same thing, in a way "blessed" by the language and enforcing strong typing.
0
Wim ten BrinkSelf-employed developerCommented:
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
Slick812Commented:
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
sftwengCommented:
I recommend a split among the responders
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.