Solved

function ReturnRecPtr: ^TRec

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 10 Seattle Dataset Actions 5 78
JSON on ISAPI REST Server using Delphi 2 229
System restore point 4 91
Convert GUI app into console app for Win32 Env 5 78
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…
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…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

21 Experts available now in Live!

Get 1:1 Help Now