What is the difference between this:

What is the difference between this:
IntToStr(10)
string(10)
?

Comments

  1. IntToStr(10) -> '10'
    string(10) -> E2089 Invalid typecast

    ReplyDelete
  2. Nándor Kiss string(10) works here, I am asking because I just saw it written in some code I am fixing at work. It's actually string(i + 1), but you get the point. Do you know any way it could work?

    ReplyDelete
  3. In XE it does not work:

    var
      S: string;

    begin
      S := string(16); // <-- [DCC Error] xxx.dpr(xx): E2089 Invalid typecast
    end.

    What is the Delphi version You're using?

    ReplyDelete
  4. Ok i got the point.... it's some hack to manuallty set the pointer of a string... bad way...

    ReplyDelete
  5. var
      I: Integer;
      S: string;

    begin
      I := 16;
      S := string(I + 1); <- AV here
    end.

    ReplyDelete
  6. Nvm, I debugged it and there is an empty try except hiding the raise, it's like this:

    try
      doSomething;
    except
    end;

    procedure doSomething;
    begin
      [code...]
      string(i+1);
      [...code]
    end;


    Great way to break things...

    ReplyDelete
  7. That reminds me of storing strings in the TComponent.Tag property....

    ReplyDelete
  8. Stefan Glienke  x.Tag := NativeInt(PChar('some string'))? ;)

    ReplyDelete
  9. Ingo Wagner Jr.
    That is what we (Stefan Glienke and myself) have found in our source code .. well not exactly this, but you get the idea.

    ReplyDelete

Post a Comment