網頁

顯示具有 delphi 標籤的文章。 顯示所有文章
顯示具有 delphi 標籤的文章。 顯示所有文章

2017年10月16日 星期一

String index V.S. Array index in Delphi

I'm used to develop code in C, however someday I meet the need which the code was written in Delphi.

The amazing fact in Delphi is the index on array and string differs ! This is really shock, it shocked me almost put hands in my mouth. 嚇得我吃手手...

In Delphi, Strings are 1-indexed, but arrays are 0-indexed.

也就是說如果有一個變數被設為 String 例如說 S:=Foo.Bar().AsString;
如果要存取 S 的第一個字元要使用 S[1] 才會是正確的字元。

If S is assigned as a string variable, the first character would be S[1] not S[0];

2017年8月18日 星期五

Delphi : record v.s. "packed" record

簡單結論 record 會依據結構 ( record )中最長的變數做 Alignment ,然而 packed record 不會。結果就是 packed record 佔的記憶體空間可能比 record 來的少。

範例
type
  // Declare an unpacked record
  TDefaultRecord = Record
    name1   : string[4];
    floater : single;
    name2   : char;
    int     : Integer;
  end;

  // Declare a packed record
  TPackedRecord = packed Record
    name1   : string[4];
    floater : single;
    name2   : char;
    int     : Integer;
  end;

var
  defaultRec : TDefaultRecord;
  packedRec  : TPackedRecord;

begin
  ShowMessage('Default record size = '+IntToStr(SizeOf(defaultRec)));
  ShowMessage('Packed record size = '+IntToStr(SizeOf(packedRec)));
end;

結果
   Default record size = 20
   Packed record size = 14

P.S. I didn't compile this code, I don't know why the size is 20 and 14. But the concept is reasonable.

2017年8月17日 星期四

Delphi : Class Method vs Object Method

簡單理解 Class Method 像是 C++ 中的 class constructor。而 Object Method 則是一般 C++ 宣告 Class 實體物件之後呼叫的 method。

最簡單的來說 Class Method 可以不經過宣告實體 object 就能被呼叫
例如說
    MyObject := MyClass.Create(  );
然而 Class Method 也可以從宣告過的實體物件被呼叫
    MyObject.MyClass( );

但是 Object Method 則必須從已經宣告過的物件來執行
    MyObject.MyMethod;

參考這網頁說明 "Understanding Class Methods"

Python TypeError: 'module' object is not callable

程式碼其實相當簡單 import random random.random() 那問題出在哪?出在當初的檔名取名為 random.py 結果造成 Python 在 import 的時候造成問題。所以在取名Python 的檔名時,切記不要取成跟 Default Module...