德尔福传递记录动态数组的功能 [英] Delphi Passing Dynamic array of Records to function

查看:145
本文介绍了德尔福传递记录动态数组的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有记录的Dynamc阵列,我希望通过引用传递数组中的项目之一的函数

I have a Dynamc array of Records and I wish to pass one of the items in the array to a function by reference

因此​​,例如数组项目之一 - AArray [1] .arecorditem是6个字符的字符串,字符串[6]

So for example one of the array items - AArray[1].arecorditem is a string of 6 characters String[6]

功能会 -

function dosomething(var AStringVal:string):Integer;

因此​​,我将执行

So I would execute

Aresult:= dosomething(AArray[1].arecorditem);

但是当我尝试编译我得到的实际和形式VAR参数的错误类型必须相同。

however when I try to compile I get the Error Type of actual and formal var parameters must be identical.

这是可以做到的,或者我应该指定数组项字符串,然后把这个字符串传给功能。

Is this possible to do or should I assign the array item to a string and then pass the string to the function.

感谢

科林

推荐答案

您的问题标题和实际问题为不一样的,所以我给你这两个问题的概述。

Your question title and the actual question are not the same, so I'll give you an overview of both subjects.

您需要定义一个阵列类型

TMyRecord = record
  Field1: String
  Field2: String
end;

TMyRecordArray = Array of TMyRecord

function DoSomething(const ARecordArray: TMyRecordArray): Integer;

这是,如果你想传递一个的整个动态项目的给函数的数组。如果你只是想传递一个项目,你会这样定义功能:

This is if you want to pass an entire dynamic array of items to the function. If you just want to pass one item, you'd define the function like this:

function DoSomething(const ARecord: TMyRecord): Integer;

现在,如果你想的字段1的通过的值的功能,你必须定义函数为:

Now, if you want to pass the value of Field1 to the function, you would have to define the function as:

function DoSomething(const AField: String): Integer;

您不能定义参数为 VAR ,否则你会用你得到的错误结束了!

You cannot define the parameter as varor you'll end up with the error you're getting!

其他:

正如其他人一直在说,如果你使用一个固定长度的字符串的领域,你需要将它定义为,正如我在上面已经展示了 TMyRecordArray

As others have been saying, if you're using a fixed-length String for the field, you need to define it as a Type just as I have demonstrated above for TMyRecordArray.

TString6 = String[6];

使用该类型既为您的域,然后你的函数参数。

Use that Type both for your Field, and your function Parameter.

这篇关于德尔福传递记录动态数组的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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