SetLength (Invalid Argument)

edited 8:25PM 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 8:25PM
    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

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.