Solved

function ReturnRecPtr: ^TRec

Posted on 2002-05-01
4
148 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

11 Experts available now in Live!

Get 1:1 Help Now