Error in created function

3

Well, I've done a function, 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 declare this in the public:

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

And I called it:

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.

    
asked by anonymous 17.10.2014 / 16:30

1 answer

3

Your problem is 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'

See: 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 rather, since it is a specialized method for downloading files, declare by requesting an object of type TIdHTTP directly, like this:

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

About the function return

You could do a test by checking whether the file was created and then returning 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 question, then 3rd addition of content in response

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 ActualValue and / or MaximumValue variables are receiving the value 0 (zero) at some point. Test the values before calculating.

    
17.10.2014 / 16:37