Solved

function ReturnRecPtr: ^TRec

Posted on 2002-05-01
4
154 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
[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
  • 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

615 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