如何从一个Delphi函数返回数组? [英] How to return array from a Delphi function?

查看:1054
本文介绍了如何从一个Delphi函数返回数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中的功能,需要返回数组。我在几个地方发现了如何通过声明数组类型,例如做到这一点。

 键入
  TStringArray =字符串数组;

然后我声明功能

 函数SomeFunction(SomeParam:整数):TStringArray;

我的问题是试图同时具有接口和实现形式进行此设置。我如何申报我喜欢的类型,有一个函数声明与该类型的接口?


解决方案

 单元1单元;接口使用SysUtils单元;类型
  TStringArray =字符串数组;功能SomeFunction(SomeParam:整数):TStringArray;...履行功能SomeFunction(SomeParam:整数):TStringArray;
开始
  SetLength函数(结果,3);
  结果[0] ='阿尔法';
  结果[1]:='测试版';
  导致[2]:='伽玛';
结束;...结束。

的金科玉律是一个单元的接口部分介绍由单位使用的数据类型和类型类和居住在单位的职能(签字)。这是所有的其他的单位看看。实现部分包含类和函数的实现。这并不是ohter单元可见。其他单位需要关心的单元,由本单位和外单位签订的合同,而不是的实施细则中实现部分找到。

的接口

I have a function in my application that needs to return an array. I have found in a couple of places how to do this by declaring the array type, e.g.

type
  TStringArray = array of string; 

And then declaring my function

function SomeFunction(SomeParam: Integer): TStringArray;

My problem is trying to set this up in a form that has both interface and implementation. How do I declare my type and have a function declaration with that type in the interface ?

解决方案

unit Unit1;

interface

uses SysUtils;

type
  TStringArray = array of string;

function SomeFunction(SomeParam: integer): TStringArray;

...

implementation

function SomeFunction(SomeParam: integer): TStringArray;
begin
  SetLength(result, 3);
  result[0] := 'Alpha';
  result[1] := 'Beta';
  result[2] := 'Gamma';
end;

...

end.

The golden rule is that the interface section of a unit describes the data types used by the unit, and the types, classes and functions (their signatures) that reside in the unit. This is what all other units see. The implementation section contains the implementation of the classes and functions. This is not visible to ohter units. Other units need to care about the interface of the unit, the 'contract' signed by the this unit and the external unit, not the 'implementation details' found in the implementation section.

这篇关于如何从一个Delphi函数返回数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆