我在习惯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 Files\Embarcadero\RAD Studio\7.0\bin\bds.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;

Delph i2010的更多相关文章

  1. 谈谈Delph中的类和对象2---类可以理解成一种特殊的数据结构、类型转换

    三.类可以理解成一种特殊的数据结构 我们知道数据类型可以进行强制类型转换,类既然可以理解成一种数据类型,那么它也应该可以进行类型转换.比如下面代码为一个按钮(Button1)的单击事件 procedu ...

  2. Delph控制台(Console)程序添加图标和版权信息

    Delphi创建控制台(Console)程序默认是无法添加图标和版权的.经过仔细的对比窗体程序与控制台程序源码,发现窗体程序的工程文中,在uses结束begin开始的地方有一句如下代码:{$R *.r ...

  3. Delph组件如何使用自己的图标(转)

    源:http://blog.csdn.net/henreash/article/details/7298451

  4. Delph 两个对立程序使用消息进行控制通信

    在实际应用中,总是会遇到两个独立的程序进行通信,其实通信的方式有好几种,比如进程间通信,消息通信. 项目中用到了此功能, 此功能用于锁屏程序, 下面把实现的流程和大家分享一下. 1. 在锁屏程序中,自 ...

  5. ide fix pack for delph 10.2.3发布了

    http://andy.jgknet.de/blog/ide-tools/ide-fix-pack/ IDE Fix Pack是RAD Studio IDE,Win32 / Win64 / Andoi ...

  6. 自己使用 1.C语言历史以及特点。

    1. C语言的发展及特点? C在1969--1973年间与Unix操作系统同时诞生:最富创造性的时期是1972年.另一次大的变化发生在1977到1979年间,当Unix系统的可移植性得到证明时.在后一 ...

  7. windows类书的学习心得(转载)

    原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...

  8. delphi 单引号在字符串中使用方法

    可以看delph的帮助,里面有这个问题详细说明:A character string, also called a string literal or string constant, consist ...

  9. delphi xe5 android 关于文件大小的几个问答O(∩_∩)O~

    摘自:http://blogs.embarcadero.com/vsevolodleonov/2013/09/19/are-you-asking-about-app-size-by-delphi-fo ...

随机推荐

  1. 栈+括号序列+暴力枚举——cf1248D1

    这个复杂度首先就想到是n3的复杂度,n2枚举换的位置,求值在花费n复杂度 判断一个序列有多少独立的括号子串时用栈处理一下即可 /* 枚举交换两个括号的位置,然后再对新的序列判一次即可 */ #incl ...

  2. XSS的原理分析与解剖:第三章(技巧篇)**************未看*****************

    ‍‍0×01 前言: 关于前两节url: 第一章:http://www.freebuf.com/articles/web/40520.html 第二章:http://www.freebuf.com/a ...

  3. thinkphp5选择redis库,让数据存入不同的redis库

    thinkphp5选择redis库,让数据存入不同的redis库 在登录的时候把个人信息存入redis,选择redis库1号库, db1 读取redis里面的个人信息

  4. 1、什么是cookie?

    什么是cookie? Cookie 定义    “Cookie”是小量信息,由网络服务器发送出来以存储在网络浏览器上,从而下次这位独一无二的访客又回到该网络服务器时,可从该浏览器读回此信息.这是很有用 ...

  5. linux 编译指定库、头文件的路径问题(转)

    1. 为什么会出现undefined reference to 'xxxxx'错误? 首先这是链接错误,不是编译错误,也就是说如果只有这个错误,说明你的程序源码本身没有问题,是你用编译器编译时参数用得 ...

  6. 二、springcloud微服务测试环境搭建

    版本说明: springcloud:Greenwich.SR3 springboot:2.1.8 1.构建步骤 1.1.microservicecloud整体父工程Project 新建父工程micro ...

  7. PCA降维-最大,最小方差解释

    转自http://www.cnblogs.com/jerrylead/archive/2011/04/18/2020209.html http://www.cnblogs.com/jerrylead/ ...

  8. winfrom创建转圈等待窗体

    第一步:创建一个WaitForm public partial class WaitForm : Form { ; private ArrayList images = new ArrayList() ...

  9. Django-ORM初识

    Django之ORM基础 一.ORM简介: ORM概念: 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术 ...

  10. Thymeleaf语法总结

    Thymeleaf是Spring boot推荐使用的模板引擎. 一.th属性 html有的属性,Thymeleaf基本都有,而常用的属性大概有七八个.其中th属性执行的优先级从1~8,数字越低优先级越 ...