* * *

Author Topic: function error  (Read 1891 times)

skaner1900

  • Jr. Member
  • **
  • Posts: 54
function error
« on: July 30, 2010, 08:55:20 pm »
Code: [Select]
function DECtoHEX(number:integer):string;
.
.
.
function Form1.DECtoHEX(number:integer):string;   <- THIS IS LINE 49   ! ! !
var a:integer; b,c:string;
begin
   repeat
   a:=number mod 16;
   number:=number div 16;
   case a of 10 : b:='A'; 11 : b:='B';12 : b:='C';13 : b:='D';14 : b:='E';15 : b:='F';
          else b:=inttostr(a);
        end;
   c:=concat(b,c)
   until number=0;
   result:=c;
end;

error:
unit1.pas(49,24) Error: class identifier expected

why?

eny

  • Hero Member
  • *****
  • Posts: 770
Re: function error
« Reply #1 on: July 30, 2010, 09:07:29 pm »
Better use Class Completion; look it up, it's useful  8)

There is no 'T' in front of 'Form1':
Code: [Select]
function TForm1.DECtoHEX(number:integer):string;
Class Completions takes care of all this tedious stuff...

Try so simplify your code:
Code: [Select]
function TForm1.DECtoHEX(number:integer):string;
const HEXCHAR: string[16] = '0123456789ABCDEF';
begin
  result := '';
  repeat
    result := HEXCHAR[number mod 16 + 1] + result;
    number := number div 16;
   until number=0;
end;

or

Code: [Select]
function TForm1.DECtoHEX(number:integer):string;
begin
  result := format('%x',[number])
end;
« Last Edit: July 30, 2010, 09:19:14 pm by eny »
WinXP Prof SP3; Lazarus 0.9.30; FPC 2.4.4; 2011-06-02 (#29749)

skaner1900

  • Jr. Member
  • **
  • Posts: 54
Re: function error
« Reply #2 on: July 30, 2010, 09:28:03 pm »
oo :D
it's so easy... :)
thank you !

skaner1900

  • Jr. Member
  • **
  • Posts: 54
Re: function error
« Reply #3 on: July 31, 2010, 01:19:20 pm »
or...

Code: [Select]
function TForm1.DECtoHEX(number:integer):string;
var
s:string;
begin
  s := format('%x',[number]);
  if length(s) =1 then s:='0'+s;
  result:=s;
end;

if you need format #000000, for exemple to html or BB code (font color)

Martin_fr

  • Hero Member
  • *****
  • Posts: 1120
Re: function error
« Reply #4 on: July 31, 2010, 02:07:53 pm »
Code: [Select]
function TForm1.DECtoHEX(number:integer):string;
  s := format('%x',[number]);
  if length(s) =1 then s:='0'+s;
if you need format #000000, for exemple to html or BB code (font color)

If you need a specific len:
uses SysUtils;

s:= IntToHex(Num, len);



 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads