Solved

function ReturnRecPtr: ^TRec

Posted on 2002-05-01
4
151 Views
Last Modified: 2010-04-04
Here is my simplified setup, using aliases in place of the real names:

type
  TRec = record
    Part1: Integer;
    Part2: String;
  end;

  TAClass = class(TObjectDescendent)
  ...
  protected
    RecArray: array of TRec;
    function ReturnRecPtr: ^TRec;
  ...
  end;

function TAClass.ReturnRecPtr: ^TRec;
var
  Index: Integer;
  Found: Boolean;
begin
  Found := False;
  if Length( RecArray ) > 0 then
  begin
    { Go through RecArray to find something. }
  end;
  if Found then
    Result := @(RecArray[ Index ])
  else
    Result := nil;
end;

I get an error compiling this sort of thing.  The error is right at the method declaration within the class and it's complaining about the ^ being there.  It complains again in the method definition.  To be more specific, the error is:
[Error] MyUnit.pas({line}): Identifier expected but '^' found

Please don't tell me I have to define a pointer type
(i.e. TRecPtr = ^TRec) unless that's what I REALLY REALLY have to do.  This approach just doesn't seem well-rounded to me because it would require a pointer type for every type of record that I'd like to return a pointer to from a function.
0
Comment
Question by:Pummel
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Cesario
ID: 6984305
Hello Pummel,

change your code :

 
 
  Delphi Programming Question  
Ask A Question   Other Questions   Reload Question    
Question: function ReturnRecPtr: ^TRec Date: 05/01/2002 12:35PM PST  
From: Pummel
Status: Waiting for Answer  Points: 50
 Email A Friend  
Here is my simplified setup, using aliases in place of the real names:

type
 PRec = ^TRec;
 TRec = record
   Part1: Integer;
   Part2: String;
 end;

 TAClass = class(TObjectDescendent)
 ...
 protected
   RecArray: array of TRec;
   function ReturnRecPtr: PRec;
 ...
 end;
 

good luck
0
 

Author Comment

by:Pummel
ID: 6984334
Well, I have another procedure that does this:

TAClass.AProcedure;
var Sample: ^TRec;
begin
  Sample := ReturnRecPtr;
  if Sample <> nil then
    { do something with Sample }
end;

If I return a record instead of a pointer to record, I will never know if the result is meaningful.
0
 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
ID: 6984363
You have 2 choices:

(1) Return a full record, but add a "IsValid" flag into the record. This way you know whether the result is meaningful or not.
(2) Return a pointer for a meaningful result and "nil" else. In this case there simply is no other way than declaring a pointer type, I'm sorry. The "^" character is really only allowed when declaring types in a "type" statement, or when dereferencing pointers in the code. You can not use it in procedure/function declarations, neither for parameters nor for return values.

Regards, Madshi.
0
 

Author Comment

by:Pummel
ID: 6984417
Well, I was just hoping for the equivalent of C++ here:

*TRec ReturnRecPtr();

Thank you.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

820 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