Solved

how to get the record type of a pointer?

Posted on 2004-11-02
265 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
Question by:edeaux
    12 Comments
     
    LVL 7

    Expert Comment

    by:sftweng
    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
    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
    >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
     
    LVL 7

    Expert Comment

    by:sftweng
    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
    can you give an example?
    0
     
    LVL 27

    Accepted Solution

    by:
    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
    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 33

    Assisted Solution

    by:Slick812
    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
    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
    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 33

    Expert Comment

    by:Slick812
    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
    I recommend a split among the responders
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    931 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now