简体   繁体   English

Delphi:未定义的记录变量

[英]Delphi: Undefined record variable

I have defined a TCGTable record type with the following structure: 我用以下结构定义了一个TCGTable记录类型:

type
  TCGTable = record
    x : array [1 .. MAX_POINTS] of Single;
    y : array [1 .. MAX_POINTS] of Single;
  end;

I have declared a TCGTable variable CGTable . 我已经声明了一个TCGTable变量CGTable

The variable CGTable is assigned a particular constant TCGTable record value if that record meets several runtime conditions. 如果变量CGTable满足多个运行时条件,则CGTable分配特定的常量TCGTable记录值。

If no constant TCGTable record meets these conditions, CGTable should be undefined. 如果没有恒定的TCGTable记录满足这些条件,则CGTable应该是未定义的。

Is there a Delphi 2010 built-in value I can assign to CGTable to indicate that it is undefined? 是否可以分配给CGTable的Delphi 2010内置值表示未定义? I have tried using the values nil and null , but both of these seem to be valid only for pointer or variant types. 我尝试使用值nilnull ,但是这两个值似乎仅对指针或变量类型有效。 The source will not compile with these values assigned to CGTable . 源将不会使用分配给CGTable这些值进行CGTable

I would like to inspect the variable CGTable to determine its validity instead of, for example, maintaining some additional boolean validity flag. 我想检查变量CGTable以确定其有效性,而不是例如维护一些附加的布尔有效性标志。

The only workarounds I can determine are: 我可以确定的唯一解决方法是:

a) Change the type of CGTable to a TCGTable pointer ( CGTable : ^TCGTable; ), which would then allow me to compare CGTable to the nil value. a)将CGTable的类型更改为TCGTable指针( CGTable : ^TCGTable; ),这将允许我将CGTablenil值进行比较。

b) Define some constant TCGTable record to act as an "invalid" record. b)定义一些恒定的TCGTable记录以充当“无效”记录。 I would then compare CGTable against this "invalid" record. 然后,我将CGTable与该“无效”记录进行比较。

Any suggestions on how to approach this? 关于如何处理此问题的任何建议? Thanks 谢谢

You have a record containing a static array . 您有一个包含静态数组的记录。 This particular record has a size equal to ElementCount*SizeOf(Element) . 该特定记录的大小等于ElementCount*SizeOf(Element)

Judging by the the fact that your array is sized with a MAX_POINTS constant it looks like you have a variable number of points in the array. 从数组的大小用MAX_POINTS常量确定的事实来看,数组中的点数似乎是可变的。 I think I would be inclined to switch to dynamic arrays like this: 我想我倾向于切换到这样的动态数组:

type
  TSinglePoint = record
    x: Single;
    y: Single;
  end;
  TSinglePointArray = array of TSinglePoint;

Now if you have a variable, a: TSinglePointArray , then a value of nil indicates that it is empty or nil. 现在,如果您有一个变量, a: TSinglePointArray ,则值为nil表示它为空或为nil。 You can query for the length of the array with Length(a) . 您可以使用Length(a)查询数组的Length(a) You can resize the array with SetLength(a, NewLength) . 您可以使用SetLength(a, NewLength)调整数组的大小。

No, your record holds as many bytes as its members take up.[*] If any combination of four bytes is a valid Single, and no more bytes are available for additional information, it becomes impossible to store the fact that the record is invalid somewhere. 不,您的记录所保存的字节数与其成员占用的字节数相同。[*]如果四个字节的任意组合是有效的Single,并且没有更多的字节可用于其他信息,则无法存储记录无效的事实某处。

You could decide that to mark a TCGTable as invalid, you set CGTable.x[1] to NaN. 您可以决定将TCGTable标记为无效,将CGTable.x [1]设置为NaN。 To check whether it is invalid, you can then check IsNaN(CGTable.x[1]). 要检查它是否无效,可以检查IsNaN(CGTable.x [1])。 This only works if all valid TCGTables will have x[1] set to a real value, though. 不过,只有在所有有效的TCGTables都将x [1]设置为实数值时,此方法才有效。 Or you could choose another value that is never valid for x[1]. 或者,您可以选择另一个对x [1]永远无效的值。 If there is no such value, you will have to create room for the extra information to be able to check the validity, possibly by storing a pointer to a TCGTable, possibly by adding an IsValid field to your record. 如果没有这样的值,则可能必须通过存储指向TCGTable的指针(可能是通过将IsValid字段添加到记录中)来为多余的信息创建空间以检查有效性。

[*] This is not true for all records, but almost definitely is in your case. [*]并非所有记录都是如此,但几乎可以肯定是您的情况。

If you changed your array types to dynamic arrays then you could check the lengths of them for your invalid record (both at length zero, for instance). 如果将数组类型更改为动态数组,则可以检查无效记录的长度(例如,长度均为零)。 Since you're using Delphi 2010 you could also use methods defined in your record to assign values, validate your ranges, and determine if the record is valid. 由于使用的是Delphi 2010,因此您还可以使用记录中定义的方法来分配值,验证范围并确定记录是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM