Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi Boolean Return Function Hint: Value assigned to '' never used?

I have

playerIds : Array[0..500] of string;

function isPlayerMob(check : string): boolean;
var
    i : integer;
begin
    for i := 0 to 500 do
    begin
        if ((playerIds[i] <> '') and (playerIds[i] = check)) then
        begin
            result := true;
        end;
    end;
    result := false;
end;

I get the Warning

Hint: Value assigned to 'isPlayerMob' never used

Can somebody tell me how to fix this? The error is for the

result := true;

like image 895
SSpoke Avatar asked May 03 '26 02:05

SSpoke


2 Answers

As others have told you, the value your loop is assigning to Result is being discarded because you are not exiting the function before the final assignment to Result, so it does not matter what the loop assigns.

You could assign the Result with an initial value and then reassign it as needed, or you could simply Exit after you have assigned your desired value:

function isPlayerMob(check : string): boolean;
var
  i : integer;
begin
  for i := 0 to 500 do
  begin
    if ((playerIds[i] <> '') and (playerIds[i] = check)) then
    begin
      Result := True;
      Exit; // <-- add this
    end;
  end;
  Result := False; // <-- only performed if the loop does not find a match
end;

Or, if you are using a recent Delphi version:

function isPlayerMob(check : string): boolean;
var
  i : integer;
begin
  for i := 0 to 500 do
  begin
    if ((playerIds[i] <> '') and (playerIds[i] = check)) then
      Exit(True); // <-- sets Result and exits at the same time
  end;
  Result := False; // <-- only performed if the loop does not find a match
end;
like image 71
Remy Lebeau Avatar answered May 05 '26 15:05

Remy Lebeau


This hint is because your always are assigning the false value to the function. no matter if the value if found in the loop.

try this

function isPlayerMob(const check : string): boolean;
var
    i : integer;
begin
    result := false;
    for i := 0 to 500 do
        if ((playerIds[i] <> '') and (playerIds[i] = check)) then
        begin
          result := true;
          break;
        end;
end;
like image 39
RRUZ Avatar answered May 05 '26 16:05

RRUZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!