Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7975495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:39:24+00:00 2026-06-04T08:39:24+00:00

I wrote this function wich compares string and return TRUE if it matches and

  • 0

I wrote this function wich compares string and return TRUE if it matches and FALSE if it doesn’t.

The only problem is that when I compile, I get an error saying that the result can be undefined. I know this is not a problem as there are only 2 possible outcome in this particular situation but I’m kind of a perfectionnist and I want to get better.
Can any of you enlighten me?

function filterUPC(upc: String): Boolean; 
var
  i, pos1: integer;
  Plano: TStringList;
  upcPlano: String;
begin
  Plano := TStringList.Create;
  if (fmMain.lblPlanook.Visible) and 
     not (fmMain.lblPlanook.Caption = 'INCOMPATIBLE') then
  begin
    Plano.LoadFromFile(fmMain.ebPlano.Text);
    for i := 0 to Plano.Count - 1 do
    begin
      pos1:=AnsiPos(';', Plano[i]);
      upcPlano := AnsiMidStr(Plano[i], pos1 + 1, 12);
      if (upc = upcPlano) then
      begin
        Result := TRUE;
        Break;
      end
      else if (i = Plano.Count - 1) then
      begin
        Result := FALSE;
      end;
    end;
  end
  else
  begin
    Result := FALSE;
  end;
  Plano.Free;
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:39:25+00:00Added an answer on June 4, 2026 at 8:39 am

    Your result is undefined if Plano is empty. In that case, the for-loop never executes and Result is never set.

    Also, you should really wrap the TStringList create/free in a try/finally (since you’re a perfectionist 😉

    Here’s what I would do:

    function filterUPC(upc: String): Boolean;
    var
      i, pos1: integer;
      Plano: TStringList;
      upcPlano: String;
    begin
      Result := FALSE;
      Plano := TStringList.Create;
      try
        if (fmMain.lblPlanook.Visible) and (fmMain.lblPlanook.Caption <> 'INCOMPATIBLE') then
        begin
          Plano.LoadFromFile(fmMain.ebPlano.Text);
          for i := 0 to Plano.Count -1 do
          begin
            pos1 := AnsiPos(';', Plano[i]);
            upcPlano := AnsiMidStr(Plano[i], pos1 + 1, 12);
            if (upc = upcPlano) then
            begin
              Result := TRUE;
              Break;
            end;
          end;
        end
      finally
        Plano.Free;
      end;
    end;
    

    I’ve added Result:=FALSE; to the top, and removed the else checks. The try/finally guarantees that Plano will be freed, even if an exception is raised.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this function but can't get on the problem that gives me 'segmentation
I wrote this function that creates a random string of UTF-8 characters. It works
I have this function that I wrote that is supposed to display notifications: function
I wrote this function that I want to use in a program, but for
I wrote this function that does this (easier to show than explain): (split 2
In my footer, I wrote this function... jQuery('#page').live('pageinit', function() { $('.widget ul').attr('data-inset', 'true'); $('.widget
I wrote this function that uses a binary search to look for a specific
I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000" .
I wrote this function private void richAdd(string who, string what) { string colorstring =
today i wrote this function: function zoom(obj){ var img = (!document.getElementById(obj))?false:document.getElementById(obj); var fullImage =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.