SetLength (Invalid Argument)

edited 1:49AM in FastScript
Hello,

I'm trying to make this simple script work:
var
  s: string;
begin
  s := 'hello world';
  SetLength(s, 5);  
end.
but everytime the scripter gets to the SetLength procedure it raises an Invalid Argument exception.

Looking into your code (v1.6, fs_isysrtti.pas, line 197) i read:
AddMethod('procedure SetLength(var S: Variant; L: Integer)', CallMethod5, FCatStr);
so I suppose when the S (variant) is evaluated it appears to represent a variable of the wrong type. Exception comes up at line 571 of the Variants.pas file of Delphi 7 (update 1).

Is function/procedure/method overloading supported? Can you help and/or fix the problem?

Thank you in advance.

Comments

  • edited 1:49AM
    Fix the fs_isysrtti.pas file:
    function TFunctions.CallMethod5(Instance: TObject; ClassType: TClass;
      const MethodName: String; var Params: Variant): Variant;
    var
      s: String;
      v: Variant;
    begin
      if MethodName = 'LENGTH' then
        Result := Length(Params[0])
      else if MethodName = 'COPY' then
        Result := Copy(Params[0], Integer(Params[1]), Integer(Params[2]))
      else if MethodName = 'POS' then
        Result := Pos(Params[0], Params[1])
      else if MethodName = 'DELETE' then
      begin
        s := Params[0];
        Delete(s, Integer(Params[1]), Integer(Params[2]));
        Params[0] := s;
      end
      else if MethodName = 'INSERT' then
      begin
        s := Params[1];
        Insert(Params[0], s, Integer(Params[2]));
        Params[1] := s;
      end
      else if MethodName = 'UPPERCASE' then
        Result := AnsiUppercase(Params[0])
      else if MethodName = 'LOWERCASE' then
        Result := AnsiLowercase(Params[0])
      else if MethodName = 'TRIM' then
        Result := Trim(Params[0])
      else if MethodName = 'NAMECASE' then
        Result := NameCase(Params[0])
      else if MethodName = 'COMPARETEXT' then
        Result := AnsiCompareText(Params[0], Params[1])
      else if MethodName = 'CHR' then
        Result := Chr(Integer(Params[0]))
      else if MethodName = 'ORD' then
        Result := Ord(String(Params[0])[1])
      else if MethodName = 'SETLENGTH' then
      begin
        if (TVarData(Params[0]).VType = varString) or
          (TVarData(Params[0]).VType = varOleStr) then
        begin
          s := Params[0];
          SetLength(s, Integer(Params[1]));
          Params[0] := s;
        end
        else
        begin
          v := Params[0];
          VarArrayRedim(v, Integer(Params[1]));
          Params[0] := v;
        end;
      end
    end;
    

Leave a Comment