[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

how to get the record type of a pointer?

Posted on 2004-11-02
15
Medium Priority
?
267 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
  • 5
  • 2
  • 2
  • +2
12 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 35

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 35

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

590 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