为什么两个看似相同的动态数组类型被认为不兼容分配? [英] Why are two seemingly identical dynamic array types deemed not assignment compatible?

查看:77
本文介绍了为什么两个看似相同的动态数组类型被认为不兼容分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个问题,我没有找到具体的答案,所以我想在这里提问可能会更快。

Just a little question, I'm not finding a specific answer so i guessed it might be faster to ask here.

编译器拒绝以下代码,并出现以下错误:

The compiler rejects the code below with the following error:


不兼容的类型'动态数组'和'字符串数组'

incompatible types 'dynamic array' and 'array of string'



TMailInfo = record
  FileName,
  MailAdresse,
  MailBCC,
  MailCC,
  MailBetreff: string;
  MailText,
  Anhang: array of string;
  MailAcknowledge,
  MailTXT: Boolean
end;

class function TEMail.SendOutlookCOMMail(aFileName, aMailAdresse, 
  aMailBCC, aMailCC, aMailBetreff: string; 
  aMailText, aAnhang: array of string; 
  const aMailAcknowledge, aMailTXT: Boolean): Boolean;
var
  mailInfo: TMailInfo;
begin
...
  mailInfo.MailBetreff := aMailBetreff;  // these two lines cause the error
  mailInfo.MailText := aMailText;
...
end;

我在做什么错?两者都是字符串数组,所以我不明白为什么一个看起来是动态的。

What am I doing wrong? Both are arrays of string, so I don't get why one seems to be dynamic.

推荐答案

您不能轻易将 MailText Anhang ,因为您不能声明具有兼容类型的另一个对象。那是因为您在记录声明中使用了内联动态数组类型。您确实需要使用可以命名的类型。为了更好地说明问题,请考虑以下问题:

You cannot readily assign to MailText and Anhang because you cannot declare another object with a compatible type. That's because you used a dynamic array type inline in your record declaration. You really need to use a type that can be named. To illustrate a bit better, consider this:

X: array of Integer;
Y: array of Integer;

现在 X Y 是不同的类型, X:= Y 不编译。

Now X and Y are of different types and X := Y does not compile.

另一个问题是您的开放数组参数。开放数组参数的分配不兼容。您可以逐个元素复制,但不能一次性分配数组。

The other problem is your open array parameter. An open array parameter is assignment compatible with nothing. You can copy element by element, but you cannot assign the array in one go.

最好的方法是这样声明字段:

The best way out of this is to declare the field like this:

MailText,
Anhang: TArray<string>;

然后将函数中的开放数组参数更改为该类型。

And change the open array parameters in the function to also be of that type.

然后,您需要确定是否要复制引用或数组:

Then you need to decide whether you want to copy the reference or the array:

MailText := aMailText; // copy reference, or
MailText := Copy(aMailText); // copy array

这篇关于为什么两个看似相同的动态数组类型被认为不兼容分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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