6

Estou com problemas em chamar a procedure seguinte no meu 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;

No botão salvar:

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

Estou com erro na linha a cima:

E2010 Incompatible types: 'Integer' and 'procedure, untyped pointer or untyped parameter'

Se mudarmos o tipo da variável ValorLinha para String o erro persiste.

1
  • Uma procedure não retorna valor e você esta tentando atribuir o retorno à variavel ValorLinha. Você pode transformar a procedure em function e retorna valor ou rever sua lógica. 24/08/2017 às 21:18

2 Respostas 2

3

Exatamente como informado no comentário, você precisa de uma função para retornar o valor desejado, ou alimentar uma variável no procedimento!

Função:

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;

Ou modificando o procedimento para alimentar uma variável externa:

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;

No botão salvar:

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

Para todos os casos a melhor opção e correta é a função!

0

Você poderia criar mais um parâmetro na procedure, para alimentar a variável.

Exemplo:

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;

No botão Salvar:

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

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .