delphi open arrays和dynamic arrays區別

genispan發表於2010-12-17

最近在寫一個應用程式的時候遇到如下問題(由於與下面問題類似,直接轉帖:)

================================================

I'm trying to resize an array of a certain class passed as an argument, e.g.

However, this raises an error "E2008 Incompatible types".

=================================================

產生此問題的原因主要為此處的Resize函式既可以接收靜態陣列引數,也可以接收動態資料引數,故SetLength方法不可使用。

解決方法為:重新定義陣列型別,如:

================================================

================================================

以上方法告訴編譯器傳入的為動態陣列,此時將可編譯。

參考資料:

<!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋體; mso-font-kerning:1.0pt;} h3 {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; mso-outline-level:3; font-size:13.5pt; font-family:宋體; mso-bidi-font-family:宋體; font-weight:bold;} p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋體; mso-bidi-font-family:宋體;} code {mso-ansi-font-size:12.0pt; mso-bidi-font-size:12.0pt; font-family:宋體; mso-ascii-font-family:宋體; mso-fareast-font-family:宋體; mso-hansi-font-family:宋體; mso-bidi-font-family:宋體;} pre {margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt; font-size:12.0pt; font-family:宋體; mso-bidi-font-family:宋體;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->

Confusion

Although the syntax is unfortunately very similar, an open array parameter should not be confused with a Delphi dynamic array . A dynamic array is an array that is maintained by Delphi, and of which you can change the size using SetLength . It is declared like:

type TIntegerArray = array of Integer;

Unfortunately, this looks a lot like the syntax used for open array parameters. But they are not the same . An open array parameter will accept dynamic arrays like array of Month , but also static arrays like array[0..11] of Month . So in a function with an open array parameter, you can't call SetLength on the parameter. If you really only want to pass dynamic arrays, you'll have to declare them separately, and use the type name as parameter type.

  
type


TMonthArray = array of

 Month;  

 
procedure

 AllKinds(const

 Arr: array of

 Month);
procedure

 OnlyDyn(Arr: TMonthArray);
Procedure AllKinds will accept static arrays as well as dynamic arrays, 
so SetLength

 can't be used, since static arrays can't be reallocated. Procedure OnlyDyn will only accept dynamic arrays, so you can 
use SetLength

 here (this will however use a copy, and not change the original array; if you want to change the length of the original array, 
use var Arr: TMonthArray
 in the declaration).

相關文章