array of const

來源:互聯網
上載者:User
  1. {A common error when working with PChars is to store a local variable in a data structure, or return it as a value. When your routine ends, the PChar disappears because it is a pointer to memory, and not a reference counted copy of the string. For example:}
  2. function title(n: Integer): PChar;
  3. var
  4.   s: string;
  5. begin
  6.   s := Format('title - %d', [n]);
  7.   Result := PChar(s); // DON'T DO THIS
  8. end;
  9. {This example returns a pointer to string data that is freed when the title function returns.}
  10. procedure TForm1.MyDataAfterDelete(DataSet: TDataSet);
  11. begin
  12.   StatusBar1.SimpleText := Format('There are now %d records in the table', [DataSet.RecordCount]);
  13. end;

format 函數的原型是 function Format(const Format: string; const Args: array of const): string; overload;

第二個參數的類型是array of const,不知道是什麼類型,搜了下:

 

變體開放數組參數  
     
   
  變體開發數組參數允許向單個過程或函數傳遞不同類型運算式的數組。要定義含有變體開放數組參數的常式,需要指定array   of   const作為參數類型。如,  
   
  procedure   DoSomething(A:   array   of   const);  
   
  這裡聲明了一個叫做DoSomething的過程,該過程可以操作不同種類的數組。  
   
  array   of   const結構等價於array   of   TVarRec。TVarRec,在單元System中聲明,用於表示一個記錄,記錄中有一個可以儲存多種值(整數、布爾、字元、實數、串、指標、類、類引用、介面、變體等)的變體部分。TVarRec中的VType欄位表示數組中每個元素的類型。一些類型作為指標傳遞而不傳遞值;特別是長串,作為指標傳遞並且必需轉換為string類型。  
   
  下面的例子在函數中使用了變體開放數組參數,該函數對接受的每個元素建立一個串標記法,最後串連成一個串。該函數中調用的串處理常式都定義在SysUtils單元中。  
   
  function   MakeStr(const   Args:   array   of   const):   string;  
   
  const  
   
      BoolChars:   array[Boolean]   of   Char   =   ('F',   'T');  
   
  var  
   
      I:   Integer;  
   
  begin  
   
      Result   :=   '';  
   
      for   I   :=   0   to   High(Args)   do  
   
          with   Args[I]   do  
   
              case   VType   of  
   
                  vtInteger:         Result   :=   Result   +   IntToStr(VInteger);  
   
                  vtBoolean:         Result   :=   Result   +   BoolChars[VBoolean];  
   
                  vtChar:               Result   :=   Result   +   VChar;  
   
                  vtExtended:       Result   :=   Result   +   FloatToStr(VExtended^);  
   
                  vtString:           Result   :=   Result   +   VString^;  
   
                  vtPChar:             Result   :=   Result   +   VPChar;  
   
                  vtObject:           Result   :=   Result   +   VObject.ClassName;  
   
                  vtClass:             Result   :=   Result   +   VClass.ClassName;  
   
                  vtAnsiString:   Result   :=   Result   +   string(VAnsiString);  
   
                  vtCurrency:       Result   :=   Result   +   CurrToStr(VCurrency^);  
   
                  vtVariant:         Result   :=   Result   +   string(VVariant^);  
   
                  vtInt64:             Result   :=   Result   +   IntToStr(VInt64^);  
   
          end;  
   
  end;  
   
  可以用一個開放數組構造器(見開放數組構造器)來調用該函數。例如:  
   
  MakeStr(['test',   100,   '   ',   True,   3.14159,   TForm])  
   
  上面的調用將返回串   “test100   T3.14159TForm"。  

也就是說[]符號就可以將任何可以format類型的變數括起來直接在format中使用了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.