delphi open arrays和dynamic arrays區別
最近在寫一個應用程式的時候遇到如下問題(由於與下面問題類似,直接轉帖:)
================================================
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).
相關文章
- Arrays
- 六,Arrays
- 【java】Arrays類Java
- Arrays工具類
- Java中Arrays作用Java
- Java Arrays.sort()Java
- java Arrays陣列Java陣列
- 18_Arrays類
- Arrays.copyOf 函式函式
- Java容器工具類ArraysJava
- Swift-陣列(Arrays)Swift陣列
- JAVA基礎--Arrays類Java
- Arrays.asList存在的坑
- 集合框架3-Arrays 類框架
- Arrays.asList()是個坑
- Arrays.asList():使用指南
- JavaSE基礎:Arrays工具類Java
- 常見物件-Arrays工具類物件
- Java中Arrays的asList()方法Java
- Chapter 1 Arrays and Strings - 1.7APT
- System.arraycopy和Arrays.copyOf的原理解剖
- 【Java】Arrays.copyOf & System.arraycopyJava
- Swift-Median of Two Sorted ArraysSwift
- LeetCode | 349 Intersection Of Two ArraysLeetCode
- 14.Java-Arrays(類)、基本型別包裝類、Integer(類)Java型別
- Java 中,Arrays 轉 List 的那些坑Java
- Arrays.sort(arr)是什麼排序排序
- CodeForces 1417B Two Arrays
- Leetcode Median of Two Sorted ArraysLeetCode
- java.util.Arrays 快速使用介紹Java
- PHP4使用者手冊:資料型別->arrays (轉)PHP資料型別
- @synthesize @dynamic 的區別
- 關於Arrays.asList返回List無法新增和刪除?
- Leetcode 4 Median of Two Sorted ArraysLeetCode
- java Arrays.copyOf實現淺複製Java
- Java踩坑記系列之Arrays.AsListJava
- Leetcode-Median of Two Sorted ArraysLeetCode
- Arrays.Sort()中的那些排序演算法排序演算法