Delph i2010

我在习惯Delphi2010

 

一直留着一个txt文件,不晓得是干嘛的(忘记了),偶然打开一看。乖乖~ 2010 还可以这样玩。

1、循环有了新用法
procedure TForm1.Button1Click(Sender: TObject);
var s:String;ch: Char;
begin
  s:='测试一下ABCDE12345^&*()';
  for ch in s do
    Memo1.Lines.Add(ch);
end;


2、类扩展,在uses 该Unit后可直接使用
type
  TDataSetHelper = class helper for TDataSet
    procedure Hello;//新增过程
    procedure Open;//直接覆盖原来的过程
  end;

{ TDataSetHelper }

procedure TDataSetHelper.Hello;
begin
  ShowMessage('Hello');
end;

procedure TDataSetHelper.Open;
begin
  ShowMessage('Open');
//inherited SaveToFile (strFileName, TEncoding.UTF8);//也是可以的。
end;

3、由于Delphi支持Unicode,可以直接用中文写变量,和过程名。以下都是支持的。

procedure 过程;
var 变量:Integer
begin
  ShowMessage('ok');
end;

4、泛型和匿名
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  aa<T> = array of T;//泛型,还有函数泛型 比如 var p:TProc(a:Integer;b:Double);

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    CheckBox1: TCheckBox;
    Memo1: TMemo;
    procedure CheckBox1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  b: Boolean = false;
  s:String;
  p:TProc;

function Return:String;
begin
  Result:='abc';
end;

function testInt: Integer;
begin
  if b then
    Exit(10);
  Result := 20;
end;

function testStr: String;
begin
  if b then
    Exit('10');
  Result := '20';
end;

function testDouble: Double;
begin
  if b then
    Exit(12.34);
  Result := 56.78;
end;

function testControl: TButton;
begin
  if b then
    Exit(Form1.Button1);
  Result := Form1.Button2;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  p:=procedure
  begin
    ShowMessage('getInt');
  end;
  Memo1.Lines.Add(IntToStr(testInt));
  ShowMessage('show');
  p;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Lines.Add(testStr);
  ShowMessage('show');
  p;

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  p:=procedure
  begin
    ShowMessage('getDouble');
  end;
  Memo1.Lines.Add(FloatToStr(testDouble));
  ShowMessage('show');
  p;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Memo1.Lines.Add(testControl.Caption);
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  b := CheckBox1.Checked;
end;

initialization
  p:=procedure
  begin
    ShowMessage('getStr');
  end;
end.


5、新增Default函数
Default(Integer)=0
Default(string)=''
Default(T)就是泛型的值

6、提示报错,标记该函数以后不再使用
procedure DoNothing;
deprecated 'use DoSomething instead';
begin
end;


7、
● CodeGear tried to minimize your code changes, so that you might
keep at least some Ansi calls as they are without harm.
● You should try to get rid of all Ansi calls (and Wide calls) in your
program, at the cost of taking some extra time for the migration.
● In case you want to keep using the AnsiString type (which is not
recommended), use the new AnsiStrings unit.
表示如果要用AnsiString代码(要用D7的代码),就是用AnsiStrings 单元???真的有这么简单吗?

8、PChar作为指针的时候,指针计算需要修改。
或者使用PByte替换之,
或者{$POINTERMATH ON}

9、-R标记 和idecaption标记
"C:Program FilesEmbarcaderoRAD Studio7.0inds.exe" -pDelphi -rSmall -idecaption="严卫强的另一个程序"
D2010可以同时启动多个。不像D7,只能一个。
(其实也可以多个,只是如果不是“完全破解版”会出现注册过期的提示。
解决方法
找个 dent.slip 替换
注册码只能用:  
      6AMD-PKG68E-DB8PP7-9SFE  
      3QH-9QW

-rSmall 是最小内容启动,几乎不启动任何第三方控件。感觉就像是启动了一个刚装的一样。

10、
.DPROJ
XML格式,而非INI格式(.DPR)

11、有人说,在begin前面增加{$R *.RES}就能把Console程序添加exe那样的版本信息。
可是D2010中只有版本信息,而程序Application 里Icon没有办法。

12、类型不兼容,可以用泛型兼容
procedure TForm30.Button1Click(Sender: TObject);
var
array1: TArrayOf10;
array2: TArrayOf10
array3, array4: array [1..10] of Integer;
begin
array1 := array2;
array2 := array3; // Error
// E2010 Incompatible types: 'TArrayOf10' and 'Array'
array3 := array4;
array4 := array1; // Error
//

type
TGenericArray<T> = class
  anArray: array [1..10] of T;
end;
TIntGenericArray = TGenericArray<Integer>;

procedure TForm30.Button2Click(Sender: TObject);
var
  array1: TIntGenericArray;
  array2: TIntGenericArray;
  array3, array4: TGenericArray<Integer>;
begin
  array1 := TIntGenericArray.Create;
  array2 := array1;
  array3 := array2;
  array4 := array3;
  array1 := array4;
end;

13、不要乱用Move

var
  str1, str2: string;
  buffer: TBytes; // TBytes = array of Byte;
begin
  str1 := 'Hello world';
  SetLength (buffer, Length (str1));
  Move (str1[1], buffer[1], Length (str1));
  SetLength (str2, Length (buffer));
  Move (buffer[1], str2[1], Length (buffer));
  Log (str1 + ' becomes ' + str2);
end;

因为String是Unicode的,Length判断的是2个Byte,结果只Move了前一半。如下。
“Hello world becomes Hello 圠湩潤we”

可以考虑在这种情况下,考虑 String 改成 RawByteString 或者我再复制一个范例过来

14、用以下代码来习惯Unicode,特别注意SizeOf,Length,ByteLength(新增函数)
var
  str1: string;
begin
  str1 := 'foo';
  Memo1.Lines.Add ('SizeOf: ' + IntToStr (SizeOf (str1)));
  Memo1.Lines.Add ('Length: ' + IntToStr (Length (str1)));
  Memo1.Lines.Add ('StringElementSize: ' +IntToStr (StringElementSize (str1)));
  Memo1.Lines.Add ('StringRefCount: ' +IntToStr (StringRefCount (str1)));
  Memo1.Lines.Add ('StringCodePage: ' +IntToStr (StringCodePage (str1)));
  if StringCodePage (str1) = DefaultUnicodeCodePage then
    Memo1.Lines.Add ('Is Unicode');
  Memo1.Lines.Add ('Size in bytes: ' +IntToStr (Length (str1) * StringElementSize (str1)));
  Memo1.Lines.Add ('ByteLength: ' +IntToStr (ByteLength (str1)));
显示结果
SizeOf: 4
Length: 3
StringElementSize: 2
StringRefCount: -1
StringCodePage: 1200
Is Unicode
Size in bytes: 6
ByteLength: 6


以上内容主要来自于《(MarkPoint) Marco_Cantu_-_Delphi_2009_Handbook》
O(∩_∩)O 其实标题应该改成“习惯2009”才对,不过偶是一直用D7的人,一下子用Unicode版本还真不习惯。

091221

 失败,忘记写泛型了。

泛型是 D2009 里才出现的东西。

我留下一点 D2010 的源代码,应该能说明一切了。

unit Generics.Collections;

type
  TPair<TKey,TValue> = record
    Key: TKey;
    Value: TValue;
    constructor Create(const AKey: TKey; const AValue: TValue);
  end;

implementation

constructor TPair<TKey, TValue>.Create(const AKey: TKey; const AValue: TValue);
begin
  Key := AKey;
  Value := AValue;
end;

uses Generics.Collections;
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var dd:TDictionary<Cardinal,String>;
begin
  dd := TDictionary<Cardinal,String>.Create;
  dd.Add(123,'123s'); // 添加一条记录
  dd.AddOrSetValue(123,'123456s'); // 把 123 替换掉
  ShowMessage(inttostr(dd.Count));
  dd.Free;
end;

顺便,大家有没有看到?

Unit 的名字是 XXXX.XXXX的形式。(D2007也可以处理这个样子)

函数泛型

// Generic Anonymous method declarations
type
  TProc = reference to procedure;
  TProc<T> = reference to procedure (Arg1: T);
  TProc<T1,T2> = reference to procedure (Arg1: T1; Arg2: T2);
  TProc<T1,T2,T3> = reference to procedure (Arg1: T1; Arg2: T2; Arg3: T3);
  TProc<T1,T2,T3,T4> = reference to procedure (Arg1: T1; Arg2: T2; Arg3: T3; Arg4: T4);

  TFunc<TResult> = reference to function: TResult;
  TFunc<T,TResult> = reference to function (Arg1: T): TResult;
  TFunc<T1,T2,TResult> = reference to function (Arg1: T1; Arg2: T2): TResult;
  TFunc<T1,T2,T3,TResult> = reference to function (Arg1: T1; Arg2: T2; Arg3: T3): TResult;
  TFunc<T1,T2,T3,T4,TResult> = reference to function (Arg1: T1; Arg2: T2; Arg3: T3; Arg4: T4): TResult;

  TPredicate<T> = reference to function (Arg1: T): Boolean;

procedure Test(a:Integer);
begin
  ShowMessage(inttostr(a));
end;

procedure TForm1.Button1Click(Sender: TObject);
var p:TProc<Integer>;
begin
  p := Test;
  p(123);
end;

原文地址:https://www.cnblogs.com/bjxsky/p/4672167.html