我在习惯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. 11、for和range的用法

    // 27_range的使用 package main import ( "fmt" ) func main() { str := "abc" //通过for打 ...

  2. Shiro学习(4)INI配置

    之前章节我们已经接触过一些INI配置规则了,如果大家使用过如spring之类的IoC/DI容器的话,Shiro提供的INI配置也是非常类似的,即可以理解为是一个IoC/DI容器,但是区别在于它从一个根 ...

  3. luoguP1313 [NOIp2011]计算系数 [组合数学]

    题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别为 a ,b ,k , ...

  4. java——String类,时间类,格式化

    日期类 格式化

  5. xlwings结合dataframe数据的写入

    一.代码 import xlwings as xw import pandas as pd xl_path=r'***' df_path=r'***' df=pd.read_excel(df_path ...

  6. debian 8 安装 codeblocks

    CodeBlocks优点: 1.所占资源少,对电脑硬件的要求低 2.开源软件,可以查看源代码,虽然我不会修改,但是如果我做一个IDE的话,我一定会参考这个伟大的软件的. 3.最重要的是,它完全跨了三大 ...

  7. 堆、栈、方法区、静态代码块---Java

    java 堆.栈.方法区 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令) 2.jvm只有一个堆区(heap)被所有线程共享,堆中不存放基 ...

  8. 互斥锁Demo

    #include <stdio.h> #include <pthread.h> pthread_t work1Id; pthread_t work2Id; ; ; pthrea ...

  9. js实现点击按钮传值

    js实现点击按钮传值 page1源码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...

  10. layui的选项卡(tab)的问题

    当页面打开单个tab时,操作栏显示: 当页面打开多个tab时,会发现操作栏与下面第一个tab显示的操作栏类型一样,并且操作栏的按钮无作用 第一个标签操作栏显示: 产生这样的原因:使用layui时,每个 ...