Kingwolves

Impossible is nothing ...

星期二, 四月 18, 2006

Delphi 2006 (For In语句) 详解

在Delphi 2005就已经加入了For element in collection do statement语法,可以用来历遍一个集合、数组等等,下面这个是For in 支持的类型:


  • Classes.TList
  • Classes.TCollection
  • Classes.TStrings
  • Classes.TInterfaceList
  • Classes.TComponent
  • Menus.TMenuItem
  • ActnList.TCustomActionList
  • DB.TFields
  • ComCtrls.TListItems
  • ComCtrls.TTreeNodes
  • ComCtrls.TToolBar



  • 首先来看一个简单的例子:

    procedure ShowListStr(StrList: TStrings);
    var
    tmpStr: String;
    begin
    for tmpStr in StrList do
    ShowMessage(tmpStr); //这样就可以历遍整个SrList的值了,比以前用for i to do 快了很多吧
    end;
    再看看这个:(数组历遍)
    type
     TIntArray = array[0..9] of Integer;
     TGenericIntArray = array of Integer;

    var
     IntArray1: array[0..9] of Integer = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
     IntArray2: array[1..2] of TIntArray = ((11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
                        (21, 22, 23, 24, 25, 26, 27, 28, 29, 30));
     IntArrayTemp: TIntArray;
     IGenericIntArray: TGenericIntArray;
     i: integer;
    begin
     for i in IntArray1 do
     begin
      ShowMessage(IntToStr(i)); //这样便可以历遍这个数组,不用再用for High Low等等函数了
     end;

     for i in IntArray2 do //多维数组的历遍
      for i in IGenericIntArray do
      begin
       ShowMessage(IntToStr(i));
      end;
    end;

    看了后是不是觉得历遍一个数组比以前方便多了.
    再看看For in 在字符串中的应用:
    var
     C: Char;
     Str1,Str2: String;
    begin
     Str1 := 'Hello Everybody,I am Kevin...';
     Str2 := '';
     for C in S1 do
    //这样就已经历遍了一个字符串,并进行了拆解...
      Str2 := Str2 + C;

     if Str1 = Str2 then
      ShowMessage('Success!');
    end;

    再看看如何历遍集合型的数据类型的:
    type
     TMyThing = (one, two, three);
     TMySet = set of TMyThing;
     TCharSet = set of Char;
    var
     MySet: TMySet;
     MyThing: TMyThing;
     CharSet: TCharSet;
     C: Char;
    begin
     MySet := [one, two, three];
     for MyThing in MySet do
    //历遍集合
     begin
     // 做相关的处理
     end;

     CharSet := [#0..#255];
     for C in CharSet do
     begin
     // 做相关的处理
     end;
    end.

    下面这个更加有用,历遍类
    这个类必须实现一个公用的方法为:GetEnumerator(),此方法返回的是一个类.而另一个类必须实现一个公用的方法为:MoveNext()和一个名为Current的属性.方法返回类型为Boolean;看例子:
    type
     TMyIntArray = array of Integer;

     TMyEnumerator = class
      Values: TMyIntArray;
      Index: Integer;
     public
      constructor Create;
      function GetCurrent: Integer;
      function MoveNext: Boolean; //这个为实现的MoveNext方法
      property Current: Integer read GetCurrent;
    end;

    TMyContainer = class
     public
     function GetEnumerator: TMyEnumerator; //这个为实现的GetEnumerator的方法.
    end;

    constructor TMyEnumerator.Create;
    begin
     inherited Create;
     Values := TMyIntArray.Create(100, 200, 300);
     Index := -1;
    end;

    function TMyEnumerator.MoveNext: Boolean;
    begin
    if Index < High(Values) then
    begin
    Inc(Index);
    Result := True;
    end
    else
    Result := False;

    end;

    function TMyEnumerator.GetCurrent: Integer;
    begin
    Result := Values[Index];
    end;
    function TMyContainer.GetEnumerator: TMyEnumerator;
    begin
    Result := TMyEnumerator.Create;
    end;

    var
     MyContainer: TMyContainer;
     I: Integer;
     Counter: Integer;
    begin
     MyContainer := TMyContainer.Create;

     for I in MyContainer do //历遍,并把TMyEnumerator的Current进行累加...
     Inc(Counter, I);

     ShowMessage(IntToStr(Counter)); //值为600
    end.
    从上面可以看到通过Current就可以实现很大功能了,我们只需把Current的类型变一个,就可以做到很方便的功能.
    总结,从For In语法看到,可以给我们编码时带来很多方便...


    星期四, 四月 06, 2006

    使用Delphi 2006创建简单WinCE程序

    在此前已经写过一篇关于使用Delphi 2005 + Delphi for .NET Compact Framework Technology Preview来创建简单的WinCE程序,那篇文章地址为: http://www.delphifans.com/infoview/Article_691.html,升级到Delphi 2006后,Borland已经把Delphi for .NET Compact Framework Technology Preview 集合到BDS 2006里面去了,下面就来创建一个简单的Demo:
    准备工作:需要Microsoft Studion .NET 2003 里面的CompactFrameworkSDK文件.
    首先新建一个Windows Forms Application - Delphi for .NET的工程,在窗体上面放一个Button和一个TextBox,双击Button,在代码下写入
    procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
    begin
    TextBox1.Text := 'Hello World!';
    end;
    写完代码后,保存工程,我们就可以编绎了,不过编绎之前我们找到
    Initializecomponent 这个初始化过程这里
    要把下列出现的方法和属性注释掉(这是因为WinCE程序里面没有这几个属性):
    SuspendLayout;
    Name;
    TabIndex;
    AutoScaleBaseSize;
    ResumeLayout(False);
    打开CMD窗口,我的工程保存在C:\Project\Demo.dpr
    在命令行下输入
    c:\Project>dccil -DCF Demo.dpr -U"C:\Program Files\Borland\BDS\4.0\lib\cf" -lu"C:\Program Files\Microsoft Visual Studion .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\System.dll"; C:\Program Files\Microsoft Visual Studion .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\System.Windows.Forms.dll"
    回车编绎。成功编绎后,放到Microsoft Windows CE 5.0虚拟机下动行就行了.
    大家可以参考Borland的例子: >
    http://bdntv.borland.com/bds/2006/CFinBDS2006.html 

    Hello,Everybody...

    Hello,Everybody:
    Welcome to me blog,I am Kevin,my bolg built in 4.06.2006,heyhey....
    This is my team logo(left)... full name is Dream three Peoples, short name is D.T.P...
    The underside logo is my individual logo...my fisrt name is Kevin,last name is Lu,chinese name is Lu Min,my birthday is 1987-11-26...
    i like computer and basketball....