问题描述
我已经在
有关数据类型tbytes和tidbytes之间的兼容性问题。
从我学到的第二个答案中可以看出,即使它们都是字节数组,它们似乎也无法一起使用。
但是,最新答案说,在Indy 10.5.9中,它取决于TBytes的存在,并且仅在Indy 10.6中,它才完全以字节数组形式提交。
无论如何,我有一个.pas单元,它可以解码来自IdUDPServerUDPRead事件的多个数据包,但无法将它们放在一起。
我总是会收到错误:
[dcc32错误] Unit1.pas(216):E2250没有可以用这些参数调用的'Unpack'重载版本
I already saw post at Delphi XE4 Indy compatibility issue between TBytes and TidBytesabout compatibility issues between the data types tbytes and tidbytes.From the second answer I learned, that it looks like they can't be used together even though they're both array of byte.However, the latest answer says, that in indy 10.5.9 it was dependent of the presence of TBytes, and that only in Indy 10.6 was it completely submitted as array of byte.Anyway,I have a .pas unit which decodes several packets from IdUDPServerUDPRead event, but can't get them together.I always get the error:[dcc32 Error] Unit1.pas(216): E2250 There is no overloaded version of 'Unpack' that can be called with these arguments
但正确地声明了Unpack:
but the Unpack is declared correctly:
class function Unpack(Bytes: TBytes; Count: Integer): TOSCPacket; overload;
class function Unpack(Bytes: TBytes; Offset, Count: Integer; TimeTag: Extended
= 0): TOSCPacket; overload; virtual;
据我所知,我的用法也是如此:
And as far as I'm aware, so is my usage of it:
OSCPacket.Unpack(AData, Length(Adata));
其中AData是字节数组。
where AData is array of byte.
我在这里看不到什么错?
我已经搜索了几个小时,无法找到一种方法来合并,转换,复制,移动或其他任何方式,将数据从AData转换为实际可用的变量,以将其放入Unpack函数的参数列表中
What am I doing wrong here, that I don't see?I've been googling for hours now, and can't find a way to merge, convert, copy, move or whatever, the data from AData to the actual usable variable for putting it in the parameter list for Unpack function.
任何帮助将不胜感激。
Any help would be more than appreciated.
谢谢,
MarcS
推荐答案
两个 Unpack
方法接收的类型为 TBytes
的参数。因此,您需要传递该类型的变量。您正在传递类型为字节数组
的变量,该变量与 TBytes
的分配不兼容。
The two Unpack
methods receive parameters of type TBytes
. So you need to pass variables that are of that type. You are passing variables of type array of Byte
which is not assignment compatible with TBytes
.
通过声明变量为 TBytes
而不是 Byte
。
Fix the problem by declaring your variables to be TBytes
instead of array of Byte
.
Delphi中的类型兼容性有点混乱。就我个人而言,我总是使用通用动态数组,它具有更宽松的兼容性规则。因此,如果我可以控制所有涉及的代码,那么我将选择使用 TArray< Byte>
而不是 TBytes
。
Type compatibility in Delphi is a bit of a mess. Personally, I always use the generic dynamic array which has more relaxes compatibility rules. So I would choose to use TArray<Byte>
rather than TBytes
if I was in control of all the code involved.
您的另一个选择是使用开放数组,这是最灵活的参数。
Another option for you is to use open arrays which are the most flexible parameters. For instance.
class function Unpack(const Bytes: array of Byte; Count: Integer): TOSCPacket;
该函数可以传递 TBytes ,
TIdBytes
,字节数组
, TArray< Byte>
,开放数组构造函数,静态字节数组等。
That function can be passed variables of type
TBytes
, TIdBytes
, array of Byte
, TArray<Byte>
, open array constructors, static byte arrays, etc.
请注意,还应将数组参数声明为
const
避免制作副本的开销。
Note that you should also declare array parameters as
const
to avoid the overhead of making copies of them.
更新1
它很清楚,
AData
实际上是一个开放数组,而不是动态数组。在这种情况下,您应该使函数接收开放数组。
It becomes clear that
AData
is in fact an open array and is not a dynamic array. In which case you should make your function receive open arrays.
我认为您的代码在
TUDPReadEvent :
type
TUDPReadEvent = procedure(AThread: TIdUDPListenerThread; AData: array of Byte;
ABinding: TIdSocketHandle) of object;
在这种情况下,
TIdBytes
不相关,这里没有这种类型的东西。并且 AData
不是动态数组,它是一个开放数组参数。因此,您还需要声明您的函数以使用开放数组。
In which case
TIdBytes
is not relevant, there is nothing of that type here. And AData
is not a dynamic array, it is an open array parameter. So you will need to declare your functions to use open arrays also.
请参见Remy的评论:正是Emba搞砸了
See Remy's comment: it was Emba that messed this up.
您应该阅读,以确保您完全了解开放数组参数与动态数组之间的区别。
You should read the documentation of open array parameters to make sure that you fully understand the difference between an open array parameter and a dynamic array.
更新2
如果您不能修改这些接口中的任何一个,则只需在它们之间放置一个适配器即可。例如:
If you cannot modify either of these interfaces, then you must simply place an adapter between them. For instance:
function CopyBytes(const Bytes: array of Byte): TBytes;
var
Count: Integer;
begin
Count := Length(Bytes);
SetLength(Result, Count);
if Count > 0 then
Move(Bytes[0], Result[0], Length(Bytes));
end;
这篇关于字节和tidbyte之间的Delphi XE3 indy兼容性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!