Incompatible data types when calling Procedure

Asked

Viewed 155 times

6

I’m having trouble calling the next trial in my form:

procedure TDM_Maquinas.IBDSMaquinasCOD_LINHASetText(Sender: TField; const Text: string);
begin
  if Text <> '' then
  begin
    if Text = 'Linha 1' then
      sender.AsInteger := 1
    else if Text = 'Linha 2' then
      Sender.AsInteger := 2
  end;
end;

On the save button:

procedure TForm.salvar(Sender: TObject);
var
 ValorLinha : Integer;
begin
 ValorLinha := DM_Maquinas.IBDSMaquinasCOD_LINHASetText(DBComboBox2.Field, 'Linha 1'); //erro
end;

I’m on the wrong line:

E2010 Incompatible types: 'Integer' and 'Procedure, untyped Pointer or untyped Parameter'

If we change the type of the variable Valorlinha for String the error persists.

  • A precedent does not return value and you are trying to assign the return to the Linevalue variable. You can turn Procedure into Function and return value or review its logic.

2 answers

3


Exactly as stated in the comment, you need a function to return the desired value, or feed a variable in the procedure!

Function:

function TDM_Maquinas.IBDSMaquinasCOD_LINHASetText(Sender: TField; const Text: string): Integer;
begin
  if Text <> '' then
  begin
    if Text = 'Linha 1' then
      Result := 1
    else if Text = 'Linha 2' then
      Result := 2
  end;
end;

Or by modifying the procedure to feed an external variable:

procedure TDM_Maquinas.IBDSMaquinasCOD_LINHASetText(Sender: TField; const Text: string);
begin
  _VariavelExterna := 0;

  if Text <> '' then
  begin
    if Text = 'Linha 1' then
      _VariavelExterna := 1
    else if Text = 'Linha 2' then
      _VariavelExterna := 2
  end;
end;

On the save button:

procedure TForm.salvar(Sender: TObject);
var
  ValorLinha : Integer;
begin
  DM_Maquinas.IBDSMaquinasCOD_LINHASetText(DBComboBox2.Field, 'Linha 1');
  ValorLinha := _VariavelExterna; 
end;

For all cases the best and correct option is the function!

0

You could create one more parameter in the past, to feed the variable.

Example:

procedure TDM_Maquinas.IBDSMaquinasCOD_LINHASetText(Sender: TField; const Text: string; aValorLinha: Integer);
begin
  if Text <> '' then
  begin
    if Text = 'Linha 1' then
      sender.AsInteger := 1
    else if Text = 'Linha 2' then
      Sender.AsInteger := 2;
    aValorLinha := Sender.AsInteger;
  end;
end;

On the Save button:

procedure TForm.salvar(Sender: TObject);
var
 ValorLinha : Integer;
begin
  DM_Maquinas.IBDSMaquinasCOD_LINHASetText(DBComboBox2.Field, 'Linha 1', ValorLinha);
end;

Browser other questions tagged

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