Lazarus

Free Pascal => Beginners => Topic started by: JLWest on May 07, 2019, 08:13:02 pm

Title: Is this a Bug?
Post by: JLWest on May 07, 2019, 08:13:02 pm
Line 157 - Is this a bug. I don't understand why I'm getting the Not found error?

Listbox1 is declared at line 24.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  9.   StrUtils, StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     btnBuild: TButton;
  17.     Edit1: TEdit;
  18.     Edit2: TEdit;
  19.     Edit3: TEdit;
  20.     Edit4: TEdit;
  21.     Edit5: TEdit;
  22.     Edit6: TEdit;
  23.     Edit7: TEdit;
  24.     ListBox1: TListBox;
  25.     ListBox2: TListBox;
  26.     ListBox3: TListBox;
  27.  
  28.  
  29.     procedure btnBuildClick(Sender: TObject);
  30.     procedure FormCreate(Sender: TObject);
  31.     function LoadDataFromFile(const AFileName: String): Boolean;
  32.     function GetCityCountry(ARCD : String) : String;
  33.     Procedure AddCity(ACity : String);
  34.     Procedure AddCountry(ACountry : String);
  35.  
  36.   private
  37.  
  38.   public
  39.  
  40.   end;
  41.  Const
  42.    CFILENAME = 'C:\UserFiles\Apt.txt';
  43. var
  44.   Form1: TForm1;
  45.  
  46. implementation
  47.  
  48. {$R *.lfm}
  49.  
  50. { TForm1 }
  51.  
  52. procedure TForm1.FormCreate(Sender: TObject);
  53.  Var i : Integer = -1;
  54.    Found : Boolean = False;
  55. begin
  56.  ListBox1.Clear;
  57.  ListBox2.Clear;
  58.  ListBox3.Clear;
  59.  end;
  60.  
  61. procedure TForm1.btnBuildClick(Sender: TObject);
  62. begin
  63.  LoadDataFromFile(CFILENAME);
  64. end;
  65.  
  66.  
  67.  function TForm1.LoadDataFromFile(const AFileName: String): Boolean;
  68.   var
  69.    DataFile   : TextFile;
  70.    LineIn     : String = '';
  71.    RCDCity    : String[45]= '';
  72.    RCDCountry : String[45] = '';
  73.    RCD        : String = '';
  74.    iApt       : String = '';
  75.    RCDCTR   : Integer;
  76.    FmtLine  : String[185];
  77.    LNDCTR   : Integer = -1;
  78.    SEACTR   : Integer = -1;
  79.    HELICTR  : Integer = -1;
  80.    nRCDCTR : String[5];
  81.    CityCtr : Integer = -1;
  82.    CityFound : Boolean = False;
  83.    CountryFound : Boolean = False;
  84.    begin
  85. //      Listbox1.Clear;
  86.       Result := True;
  87.       LNDCTR    :=  0;
  88.       SEACTR    :=  0;
  89.       HELICTR   := 0;
  90.       RCDCTR    := 0;
  91.       CityCtr   := 0;
  92.       AssignFile(DataFile, AFileName);
  93.       try
  94.         Reset(DataFile);
  95.         while not eof(DataFile) do  begin
  96.           Readln(DataFile, LineIn);
  97.           RCD := Copy2Space(LineIn);
  98.           Inc(RCDCTR);
  99.           Edit1.Text := IntToStr(RCDCTR);
  100.           iApt := ExtractWord(5, LineIn, [' ']);
  101.           Case RCD of
  102.            '1'  : begin
  103.                    nRCDCTR := IntToStr(RCDCTR);
  104.                    FmtLine := iApt + '  ' + nRCDCTR;
  105.                    ListBox1.Items.Add(FMTLine);
  106.                    Inc(LNDCTR);
  107.                    Edit2.Text := IntToStr(LNDCTR);
  108.                    Continue;
  109.                    end;
  110.  
  111.            '16' : Begin
  112.                    nRCDCTR := IntToStr(RCDCTR);
  113.                    FmtLine := iApt + '  ' + nRCDCTR;
  114.                    ListBox1.Items.Add(FMTLine);
  115.                    Inc(SEACTR);
  116.                    Edit3.Text := IntToStr(SEACTR);
  117.                    Continue;
  118.                   end;
  119.  
  120.            '17' : begin
  121.                    nRCDCTR := IntToStr(RCDCTR);
  122.                    FmtLine := iApt + '  ' + nRCDCTR;
  123.                    ListBox1.Items.Add(FMTLine);
  124.                    Inc(HELICTR);
  125.                    Edit4.Text := IntToStr(HELICTR);
  126.                    Continue;
  127.                    end;
  128.  
  129.         '1302'  : begin
  130.                    CityFound := IsWordPresent('city', LineIn, [' ']);
  131.                    if CityFound then begin  RCDCity := GetCityCountry(LineIn); end;
  132.                    if CityFound then begin AddCity(RCDCity); end;
  133.                    CountryFound := IsWordPresent('country', LineIn, [' ']);
  134.                    if CountryFound then begin RCDCountry := GetCityCountry(LineIn); end;
  135.                    if CountryFound then begin AddCountry(RCDCountry); end;
  136.                    FmtLine := iApt + '  ' + nRCDCTR;
  137.                    Inc(HELICTR);
  138.                    Edit4.Text := IntToStr(HELICTR);
  139.                    Continue;
  140.                    end;
  141.           end;
  142.            Application.ProcessMessages;
  143.          end;
  144.         CloseFile(DataFile);
  145.        except
  146.          Result := False;
  147.        end;
  148.    end;
  149.  
  150.  procedure AddAirport(AAirport : String);
  151.   Var i : Integer = -1;
  152.    Item : String = '';
  153.   begin
  154.     Item := AAirport;
  155.     if AAirport.IsEmpty then begin exit; end;
  156.     Item := Trim(AAirport);
  157.     i := ListBox1.Items.Indexof(Item);     // unit1.pas(157,10) Error: Identifier not found "ListBox1"
  158.     if i > -1 then begin exit; end;
  159.     Listbox1.Items.Add(Item);
  160.   end;
  161.  
  162.  Procedure TForm1.AddCity(ACity : String);
  163.   Var i : Integer = -1;
  164.    Found : Boolean = False;
  165.    Item : String = '';
  166.   begin
  167.    Item := ACity;
  168.    i := Listbox2.Items.Indexof(Item);
  169.    if i > -1 then begin exit; end;
  170.    ListBox2.Items.Add(Item);
  171.    i := Listbox2.Items.Count;
  172.    Edit5.Text := IntToStr(i);
  173.   end;
  174.  
  175.  Procedure TForm1.AddCountry(ACountry : String);
  176.   Var i : Integer = -1;
  177.    Found : Boolean = False;
  178.    Item : String = '';
  179.   begin
  180.    Item := ACountry;
  181.    i := Listbox3.Items.Indexof(Item);
  182.    if i > -1 then begin exit; end;
  183.    ListBox3.Items.Add(Item);
  184.    i := Listbox3.Items.Count;
  185.    Edit6.Text := IntToStr(i);
  186.   end;
  187.  
  188.  function TForm1.GetCityCountry(ARCD : String) : String;
  189.   var i : Integer = -1;
  190.    Item : String = '';
  191.    Bit1 : String = '';
  192.    begin
  193.     Item := ARCD;
  194.     Bit1 := Copy2SpaceDel(Item);
  195.     Item := Trim(Item);
  196.     Bit1 := Copy2SpaceDel(Item);
  197.     Item := Trim(Item);
  198.    Result := Item;
  199.    end;
  200.  
  201. end.
  202.  

Thanks
Title: Re: Is this a Bug?
Post by: howardpc on May 07, 2019, 08:24:46 pm
Make AddAirport() a method of TForm1, then ListBox1 will be within the same scope.

At the moment AddAirport is a stand-alone procedure, not connected to TForm1's fields in any way.
Title: Re: Is this a Bug?
Post by: wildfire on May 07, 2019, 08:25:15 pm
Take a closer look at line 150  ;)
Title: Re: Is this a Bug?
Post by: JLWest on May 07, 2019, 08:55:53 pm
Thank You Howardpc & Wildfire.
TinyPortal © 2005-2018