Error Char to String conversion

Asked

Viewed 247 times

2

I have the following code:

procedure TDM_Maquinas.IBQCons_MaquinasCOD_LINHAGetText(Sender: TField;
var Text: string; DisplayText: Boolean);
begin
  Text := Sender.AsString;
  if Text <> '' then
  begin
    case Text[1] of
    '1'   : Text := 'Linha 1';
    '2'   : Text := 'Linha 2';
    '3'   : Text := 'Linha 3';
    '4'   : Text := 'Linha 4';
    '101' : Text := 'Recebimento 1'; //aqui da erro
    '102' : Text := 'Recebimento 2'; //aqui da erro
    '201' : Text := 'Expedição 1';   //aqui da erro
    '202' : Text := 'Expedição 2';   //aqui da erro
    '203' : Text := 'Expedição 3';   //aqui da erro
   end;
 end;
end;

error:

E2010 Incompatible types: 'Char' and 'string'

How do I solve?

  • How could Text[1] be equal to 101.102.....?

  • @Reginaldorigo, why couldn’t it be ?

1 answer

3


When analyzing the pattern, we have that the cases are always numbers, so we could convert the value of Text to integer, and thus solve your problem:

procedure TDM_Maquinas.IBQCons_MaquinasCOD_LINHAGetText(Sender: TField;
var Text: string; DisplayText: Boolean);
begin
  Text := Sender.AsString;
  if Text <>'' then
  begin
    case StrToInt(Text) of
    1   : Text := 'Linha 1';
    2   : Text := 'Linha 2';
    3   : Text := 'Linha 3';
    4   : Text := 'Linha 4';
    101 : Text := 'Recebimento 1'; 
    102 : Text := 'Recebimento 2'; 
    201 : Text := 'Expedição 1';   
    202 : Text := 'Expedição 2';   
    203 : Text := 'Expedição 3';   
   end;
 end;
end;

Edit: As @Reginaldo commented, when using Text[1], you will only have the first character of your string, so I also changed Text[1] by Text

  • If Text[1] has only one character. How can Strtoint(Text[1]) be equal to 101,102..?

  • I traveled here, I thought text was a vector, and he was taking the first position to use in the case. Then changing the Text[1] for Text, solves the problem. Thank you @Reginaldorigo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.