1

I'm trying to pass an array out of a class into my main program in Delphi. I'm having a bit of trouble with the data types and an hour scouring the web has found nothing to help me. It sounds a bit strange, but the more complex the answer the better (it's for a college project).

I have a class connected to SQL which reads an SQL Query into an array of a record Type declarations (sorry if it's a bit messy at the moment) :

Type TScout = Record
SNum, FName, SName, Gender, Address, HomeNum, MobNum,
SEmail, STel, Hikes, Nights, Med, Diet : String;
DoB, DoJ : String;
End;

Type TScoutArray = Array of TScout;

Type TScoutSQL = Class
Public
  Procedure InitSQL;
  Procedure GetRecords;
  Function SendRecords : TScoutArray;
Private
  ScoutsArray : TScoutArray;
  ScoutConnection : TSQLConnection;
  ScoutQuery : TSQLQuery;
End;

So the whole "Function SendRecords : TScoutArray;" isn't working, as on the other side I have the same 2 types (TScout and TScoutArray) declared exactly the same, I call the function:

  ScoutArray := ScoutSQL.SendRecords;

And I get:

[Error] MembersUnit.pas(51): Incompatible types

Can anyone help?

4
  • Record type declaration means a record, right? So, you read the data from a DBMS, store it in a record and try to pass a dynamic array of records around. You should also provide more code, like the type of ScoutArray. that is because TScoutArray and array of TScout are different types in Pascal/Delphi. Commented Feb 14, 2011 at 23:47
  • I made TScoutArray because I read you can't just pass an array out of a class, you have to make your own data type, but it still didn't work, so I asked here Commented Feb 15, 2011 at 8:53
  • 2
    if you found your answer you should mark it as accepted or supply more details if we haven't understood. Commented Feb 15, 2011 at 11:08
  • @David Heffernan Apologies for that. Now marked the answer. Thanks for answering swiftly and in a way I could easily understand. Commented Feb 16, 2011 at 15:01

2 Answers 2

3

I suspect that your problem is that you are declaring these types twice in separate units. Doing so results in distinct, incompatible types.

What you need to do is to:

  • Declare the types, in the interface section, of one unit only (unit A, say).
  • In another unit (unit B, say) that wants to use these types you add unit A to the uses clause.
Sign up to request clarification or add additional context in comments.

1 Comment

or add the type declaration into the interface section of a third unit C, and include C in both A and Bs USES clauses.
2

Wrong way

var
  badArray: array of TScout;
begin
  badArray := ScoutSQL.SendRecords;

This won't work. array of TScout and TScoutArray, the latter being what SendRecords returns, are different types in Delphi/Pascal.

Right way

var
  niceArray: TScoutArray;
begin
  niceArray := ScoutSQL.SendRecords;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.