Error in created function

Asked

Viewed 210 times

3

Well, I did a job, but it’s not working. The purpose of this function is to download a file and when finished, return true.

This is the function:

function TForm1.BaixarArquivo(Sender: TObject; Url:String; Name:Integer):Boolean;
var
  fileTD : TFileStream;
begin
  fileTD := TFileStream.Create(IntToStr(Name) + ExtractFileExt(Url), fmCreate);
  try
    Sender.Get(Url, FileTD);
  finally
    FreeAndNil(fileTD);
  end;
end;

I declared so in the public:

function BaixarArquivo(Sender: TObject; Url:string; Name:Integer): Boolean;

And I called it that:

BaixarArquivo(IdHTTP1, 'http://pokestage.ddns.net/patch/5.rar', 5);

But returns this error:

[Error] Launcherunit.pas(66): Undeclared Identifier: 'Get'

Second function:

function TForm1.RetornaPorcentagem(ValorMaximo, ValorAtual: Real):string;
var 
  resultado: Real;
begin
  resultado := ((ValorAtual * 100) / ValorMaximo); Result := FormatFloat('0%', resultado);
end;

The error is from Floating Division by Zero.

1 answer

3


Your problem is on the cast.

Your function receives a TObject and this type of class does not have a Get method.
Exactly as the error message says:

[Error] Launcherunit.pas(66): Undeclared Identifier: 'Get'

Behold: Undeclared identifier: 'Get'. This is saying that the Get method is not a declared identifier.

You need to cast within the method:

TIdHTTP(Sender).Get(Url, FileTD);

Or better, since it is a specialized method for downloading files, declare asking for a Tidhttp type object directly, like this:

function BaixarArquivo(Sender: TIdHTTP; Url:string; Name:Integer): Boolean;

About the return of the function

You could do a test by checking if the file was created and then return the test result via result. It would look like this:

function TForm1.BaixarArquivo(Sender: TIdHTTP; Url:String; Name:Integer):Boolean;
var
  fileTD : TFileStream;
begin
  fileTD := TFileStream.Create(IntToStr(Name) + ExtractFileExt(Url), fmCreate);
  try
    Sender.Get(Url, FileTD);
  finally
    FreeAndNil(fileTD);
  end;
  
  result := FileExists(IntToStr(Name) + ExtractFileExt(Url));
end;

3rd Edit of the question, then 3rd addition of content in the answer

Question:

Second function:

function TForm1.RetornaPorcentagem(ValorMaximo, ValorAtual: Real):string;
var resultado: Real;
begin
  resultado := ((ValorAtual * 100) / ValorMaximo); Result := FormatFloat('0%', resultado);
end;

The error is from Floating Division by Zero.

The variables Current value and/or Maximum value are receiving the value 0 (zero) at some point. Test the values before performing the calculation.

  • Good James, tried so, now returns another error: [Warning] Launcherunit.pas(70): Return value of Function 'Tform1.Downloadable' Might be Undefined

  • But after that, the crash program. 'Process Launch.exe Raised Exception class Ezerodivide message 'Floating point Division by zero'.

  • Good, now gave problem in a function I was already created and working. I edited the post.

  • 1

    Solved. As for my mistakes, I will avoid them, I do not know much the rules.

Browser other questions tagged

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