Como converter Byte para integer no delphi?

Delphi

28/10/2004

Tenho um valor numérico em formato Byte. Como recuperá-lo em integer?


Grato.


Ronaldo

Ronaldo

Curtidas 0

Respostas

Delphi32

Delphi32

28/10/2004

Acho que é [url=http://delphiforum.icft.com.br/forum/viewtopic.php?p=167887#167887]isso[/url] que você procura...

Caso não seja, bem, não é preciso converter um valor em outro. Eles são ´compatíveis´. Teste o seguinte exemplo:

...
var s:byte;
    i:integer;
begin
   s := 1;
   i := 0;
   i := s;
end;


Até!


GOSTEI 0
Ronaldo

Ronaldo

28/10/2004

Na verdade, o delphi reclama quando mando converter o byte em integer. Diz que são tipos incompatíveis.


GOSTEI 0
Aroldo Zanela

Aroldo Zanela

28/10/2004

Colega,

Você tem um exemplo ou fragmento de código que apresenta o erro? Veja:

var A: Byte;
B: Integer;
begin
A := 1;
//  B := Integer(A);
  B := A; // Equivalente ao typeCasting efetuado acima, ou seja, não dá erro e não é necessário
  if A = B then ShowMessage(´Ok´);



GOSTEI 0
Ronaldo

Ronaldo

28/10/2004

function Get_AutoIncremento(Const Tabela: string): integer;
var Q: TQuery;
begin
Q := TQuery.Create(nil);
try
Q.DatabaseName := ´dbcfw´;
Q.sql.Text := ´select gen_id(G_´ + uppercase(tabela)+ ´,1) from RDB$DATABASE´;
Q.open;


// o BDE retorna como Byte.
// por isso do erro.
result := q.FieldByName(´gen_id´).Value;
finally
q.Free;
end;
end;


GOSTEI 0
Ronaldo

Ronaldo

28/10/2004

Consegui resolver o problema. Olha como ficou minha função:

function Get_AutoIncremento(Const Tabela: string): integer;
  var Q: TQuery;
      MyBuffer : ^integer;
begin
  Q := TQuery.Create(nil);
  try
    Q.DatabaseName := ´dbcfw´;
    Q.sql.Text := ´select gen_id(G_´ + uppercase(tabela)+ ´,1) from RDB$DATABASE´;
    Q.open;

    with q.FieldByName(´gen_id´) do
    begin

      GetMem(MyBuffer, DataSize);
      try
        if not GetData(MyBuffer) then
           MessageDlg(FieldName + ´ is NULL´, mtInformation, [mbOK], 0)
        else
           result := MyBuffer^;{ Do something with the data };
      finally
        { Free the space }
        FreeMem(MyBuffer, DataSize);
      end;
    end;

  finally
    q.Free;
  end;
end;


Grato


GOSTEI 0
POSTAR