在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Delphi的Format函数大家都用得很多,第二个参数用着确实很方便。最近在数据库开发应用中需要自己创建一个带array of const参数的函数,对于常用的类型String,Integer,Pointer处理都没什么问题,但当用到Widestring类型时却出错,摸索了一上午,感觉获益良多。现在将问题、解决问题的思路、分析方法等一一道来,希望对诸君有所启发就达到了我写这篇文章的目的了!
///环境:Winxp + D7
///进入D7,在默认的新建工程中增加一过程Test(m: Array of const); procedure TForm1.test(m: array of const); var i, zero: Integer; s, t: String; c: Char; const sBoolean: Array [Boolean] of string = ('False', 'True'); begin s := ''; for i := 0 to High(m) do with m[i] do case VType of //写到这,按住Ctrl点击VType,打开System单元,将VType的枚举值贴到Case语句 vtInteger: (VInteger: Integer; VType: Byte); vtBoolean: (VBoolean: Boolean); vtChar: (VChar: Char); vtExtended: (VExtended: PExtended); vtString: (VString: PShortString); vtPointer: (VPointer: Pointer); vtPChar: (VPChar: PChar); vtObject: (VObject: TObject); vtClass: (VClass: TClass); vtWideChar: (VWideChar: WideChar); vtPWideChar: (VPWideChar: PWideChar); vtAnsiString: (VAnsiString: Pointer); vtCurrency: (VCurrency: PCurrency); vtVariant: (VVariant: PVariant); vtInterface: (VInterface: Pointer); vtWideString: (VWideString: Pointer); vtInt64: (VInt64: PInt64); end; Delete(s, 1, 1); Self.Caption := s; end; ///继续写,对各枚举值进行处理!这里作一下解释,Array of const正是由TVarRec类型组成的! ///请看Case of语句中的代码: vtInteger: s := s + ';' + IntToStr(VInteger); vtBoolean: s := s + ';' + sBoolean[VBoolean]; vtChar: s := s + ';' + VChar; vtExtended: s := s + ';' + FloatToStr(VExtended^); vtString: if Assigned(VString) then begin t := VString^; s := s + ';' + t; end; vtPointer: if Assigned(VPointer) then s := Format('%S; Pointer: $%X ',[s, Integer(VPointer)]); vtPChar: if Assigned(VPChar) then begin t := VPChar^; s := s + ';' + t; end; vtObject: if Assigned(VObject) then s := Format('%S; $%X ClassName: %S ',[s, Integer(@VObject), VObject.ClassName]); vtClass: if Assigned(VClass) then s := Format('%S; Class Reference $%X - ClassName: %S ',[s, Integer(VClass), VClass.ClassName]); vtWideChar: begin t := VWideChar; s := s + ';' + t; end; vtPWideChar: if Assigned(VPWideChar) then begin t := VPWideChar^; s := s + ';' + t; end; vtAnsiString: if Assigned(VAnsiString) then begin t := PChar(VAnsiString); s := s + ';' + t; end; vtCurrency: if Assigned(VCurrency) then s := s + ';' + FloatToStr(VCurrency^); vtVariant: if Assigned(VVariant) then s := s + '; This is variant '; vtInterface: if Assigned(VInterface) then s := Format('%S; Interface: $%X',[s, Integer(VInterface)]); vtWideString: if Assigned(VWideString) then begin t := PWideString(VWideString)^; s := s + ';' + t; end; vtInt64: if Assigned(VInt64) then s := s + ';' + IntToStr(VInt64^);
procedure TForm1.Button1Click(Sender: TObject);
var ws: WideString; begin ws := 'dda这是一个测试dfa'; test([self, 'sdf', 2.3324, ws, TForm]); end;
http://www.cnblogs.com/sonicit/archive/2008/03/23/1118524.html |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论