开发者

Parameter Passing an Array of Type in Delphi

开发者 https://www.devze.com 2023-02-10 17:15 出处:网络
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.

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?


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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消