Philip Ellis
Philip Ellis

Reputation: 39

Delphi problems inserting a string, Incompatible types error

procedure TTelephoneNumberConverter.btnConvertClick(Sender: TObject);
var
  number: string;
  dupe: string;
  converted: string;
begin
  number := edtInput.Text ;
  dupe := Copy(number, 4, 1) ;
  converted := Insert(dupe , number , 4 ) ;
  pnlOutput.Caption := converted;
end;

Ok guys I just have a quick question regarding Delphi 2010 and inserting strings into other strings. The purpose of this small piece of code is to take the 4th character in a specific string and to duplicate it and add it next to the specific character e.g. 12345 -> 123445

The only problem is I keep getting an error :

Incompatible types 'string' and 'procedure, untyped pointer or untyped parameter'.

I am probably missing something small and stupid but would appreciate if someone could maybe answer my question.

Upvotes: 1

Views: 2203

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

Insert is a procedure that modifies its second argument.

Its signature is:

procedure Insert(Source: string; var Dest: string; Index: Integer);

The compiler error you see occurs because Insert does not return anything and thus cannot be the rhs of an assignment.

Your code should therefore be:

converted := number;
Insert(dupe, converted, 4);

Copy is overkill for a single character. Use [] instead:

dupe := number[4];

Upvotes: 2

Related Questions