Mega Code Archive

 
Categories / Delphi / Examples
 

The return of the Writeln

Title: The return of the Writeln Question: How can I use something like the old "writeln" for memolines. Answer: // Saw delphi 3000 article "How to convert values in any type // do string value ? " based on that added function sWriteln // varying number of parameters in brackets [], just a small thing unit WUtils; interface uses sysutils, classes; // Now presenting on a screen in front of YOU, to be short // .... the return of the WRITELN .... // for memo1.lines // [well almost, nobody is perfect], just basics, idea seems to work, // extra formatting options can be included. Slower then standard routines // WimdW 2010-02-02 // Example howto use: // i:=2;x:=2.2; // old: // memo1.lines.add('Old = i: '+IntToStr(i)+'x: '+FloatToStr(x)); // use ToStr, easy to remember // memo1.lines.add('ToStr= i: '+ToStr(i)+'x: '+ToStr(x)); // use SWriteln([]) for more compact code lines // memo1.lines.add(sWriteln(['sWrt= i: ',i,' x: ',x])); // use lWriteln([]) for lines.add // lWriteln(memo1.lines,['lWrt = i: ',i,' x: ',x]); function ToStr(Value: Variant): String; // writes, returns a String function sWriteln(aArgs : Array Of Const) : string; // writes, adds a Line procedure Lwriteln( lines : Tstrings; aArgs : Array Of Const); implementation // Marcelo Torres www.delphi3000.com/articles/article_1540.asp?SK= // and comments function ToStr(Value: Variant): String; begin //actually, variants know how to convert themselves to strings result:=value; (* // for more detailed formatting: case TVarData(Value).VType of varSmallInt, varInteger : Result := IntToStr(Value); varSingle, varDouble, varCurrency : Result := FloatToStr(Value); varDate : Result := FormatDateTime('dd/mm/yyyy', Value); varBoolean : if Value then Result := 'T' else Result := 'F'; varString : Result := Value; else Result := ''; end; *) end; // Rudy Velthuis www.rvelthuis.de/articles/articles-openarr.html Function ElementOfConst(aIndex: Integer; aArray: Array Of Const): Variant; begin with aArray[aIndex] do case VType of vtInteger: Result := VInteger; vtBoolean: Result := vBoolean; vtChar: Result := VChar; vtExtended: Result := vExtended^; vtString: Result := VString^; vtPChar: Result := StrPas(VPChar); vtObject: Result := VObject.ClassName; vtClass: Result := VClass.ClassName; vtAnsiString: Result := String(VAnsiString); vtCurrency: Result := VCurrency^; vtVariant: Result := VVariant^; vtInt64: Result := VInt64^; end; end; function sWriteln(aArgs : Array Of Const) : string; var i : integer; v : variant; str : string; begin str:=''; for i:=0 to High (aArgs) do begin v:=ElementOfConst(i,aArgs); str:=str+tostr(v); end; result:=str; end; procedure lWriteln( lines : Tstrings; aArgs : Array Of Const); begin lines.add(sWriteln(aArgs)); end; end.