Error comparing strings in Delphi

-2

I'm using the code:

conexao.First;
while not conexao.Eof do
  begin
  if (conexao.FieldValues['complex'] = '02' and conexao.FieldValues['financ'] = '04') then
    total24:= total24 + conexao.FieldValues['val_tot'];
  conexao.Next;
  end;

In this line if (conexao.FieldValues['complex'] = '02' and conexao.FieldValues['financ'] = '04') then I get the following error:

  

Incompatible types: 'string' and 'Boolean'

Why?

    
asked by anonymous 21.10.2018 / 15:27

2 answers

1

Generally, FieldValues['NOME_FIELD'] results is a Variant , without an appropriate typecast it can result in errors exactly the same as the one presented.

The correct way is to enter the name and type of the field, here is an example.

NOME_TABELA.FieldByName('NOME_FIELD').AsString = '02'
NOME_TABELA.FieldByName('NOME_FIELD').AsInteger = 02
NOME_TABELA.FieldByName('NOME_FIELD').AsBoolean = True

And so it goes.

    
21.10.2018 / 17:26
0

The only solution is to separate the comparisons using parentheses:

if ((conexao.FieldValues['complex'] = '02') and (conexao.FieldValues['financ'] = '04')) then

    
22.10.2018 / 03:00