?
Solved

function ReturnRecPtr: ^TRec

Posted on 2002-05-01
4
Medium Priority
?
155 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 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

719 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