Pregunta

I'm trying to use TStrings.ValueFromIndex (which works in FreePascal and Delphi) in a PascalScript function, but it doesn't work, the compiler returns:

                                        Unknown identifier 'GETVALUEFROMINDEX'

I'm using it well?
Is this functionality available in PascalScript?
if not, is there any easy way to do that?

THE CODE:

Function dummy(R: TStringList):String;
var
   i: Integer;
   RESULTv: string;
begin
   for i := 0 to ReqList.Count-1 do
     RESULTv := RESULTv + R.Names[i]+' -> '+ R.ValueFromIndex[i];
   dummy := RESULTv;
end;
¿Fue útil?

Solución 2

PascalScript's TStrings is a Delphi TStrings, but the ValueFromIndex method is not exposed by PascalScript. This can be seen by reading SIRegisterTStrings.

So, you need to make use of what is available. For instance the Values property:

RESULTv := RESULTv + R.Names[i] + ' -> ' + R.Values[R.Names[i]];

Or you might prefer to avoid some repetition with

Name := R.Names[i];
RESULTv := RESULTv + Name + ' -> ' + R.Values[Name];

This is rather inefficient, but unless you are going to parse the name/value pairs yourself, it's probably the best that you can do.

If you felt brave, you could compile PascalScript yourself, and add a call to RegisterMethod in SIRegisterTStrings that registered ValueFromIndex.

Otros consejos

PascalScript is not the same as Delphi/FreePascal. If you look at the source code for PascalScript (specifically in uPSC_classes.pas), you will see that PascalScript merely wraps a native Delphi/FreePascal TStringList, but does not expose everything that Delphi/FreePascal actually implements in TStringList. For instance, there is no wrapper exposed for the ValueFromIndex property.

Update:

Since PascalScript does not expose the ValueFromIndex property, you can write your own code that manually parses a String to remove its Name portion (if you don't patch PascalScript itself to add the missing property registration), eg:

Function GetValueFromIndex(R: TStringList; Index: Integer):String;
var
  S: string;
  i: Integer;
begin
  S := R.Strings[Index];
  i := Pos('=', S);
  if I > 0 then
    ValueFromIndex := Copy(S, i+1, MaxInt)
  else
    ValueFromIndex := '';
end;

Function dummy(R: TStringList):String;
var
  i: Integer;
  RESULTv: string;
begin
  for i := 0 to ReqList.Count-1 do
    RESULTv := RESULTv + R.Names[i] + ' -> ' + GetValueFromIndex(R, i);
  dummy := RESULTv;
end;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top