Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Tom will not be able to use this answer as his Delphi version isn't high enough, but anybody on D2010 or higher will be able to put the extended rtti's TValue to good use on this type of challenge.</p> <p>Following is a small console app showing how to:</p> <ul> <li>create a dynamic array of TValue's from a open array parameter; </li> <li>copy a dynamic array of TValue's to a new dynamic array modifying individual values on the way;</li> <li>modify the items in a dynamic array of TValues "in place".</li> </ul> <p>Enjoy.</p> <pre><code>program Project1; {$APPTYPE CONSOLE} {$R *.res} uses System.Rtti, System.SysUtils, System.TypInfo; const StringKinds: set of TTypeKind = [tkChar, tkString, tkWChar, tkLString, tkWString, tkUString]; type TValueArray = array of TValue; function ValueArrayFromConstArray(const aSource: array of TValue): TValueArray; var idx: Integer; begin SetLength(Result, Length(aSource)); for idx := Low(aSource) to High(aSource) do Result[idx] := aSource[idx]; end; function ReturnNewArray(const aSource: TValueArray): TValueArray; var idx: Integer; begin SetLength(Result, Length(aSource)); for idx := Low(aSource) to High(aSource) do if aSource[idx].Kind in StringKinds then Result[idx] := 'Dest' + aSource[idx].ToString else if aSource[idx].Kind in [tkInteger] then Result[idx] := 10 + aSource[idx].AsInteger else Result[idx] := aSource[idx]; end; procedure ModifyArrayValues(var aArray: TValueArray); var idx: Integer; begin for idx := Low(aArray) to High(aArray) do if aArray[idx].Kind in StringKinds then aArray[idx] := 'Dest' + aArray[idx].ToString else if aArray[idx].Kind in [tkInteger] then aArray[idx] := 10 + aArray[idx].AsInteger else ;//aArray[idx] := aArray[idx]; end; var Source: TValueArray; Destination: TValueArray; Item: TValue; idx: Integer; begin Source := ValueArrayFromConstArray(['Some', 42, TObject]); Destination := ReturnNewArray(Source); idx := 0; WriteLn('', #9, 'Old', #9, 'New'); WriteLn('-', #9, '----', #9, '----'); for Item in Source do begin WriteLn(idx, #9, Item.ToString, #9, Destination[idx].ToString); Inc(idx); end; WriteLn; WriteLn; WriteLn('', #9, 'Modified'); WriteLn('-', #9, '----'); Source := ValueArrayFromConstArray(['first', 50, TValue.From&lt;TFloatValue&gt;(fvCurrency)]); ModifyArrayValues(Source); for Item in Source do begin WriteLn(idx, #9, Item.ToString); end; ReadLn; end. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload