someonesomewhere
someonesomewhere

Reputation: 21

add items to a listbox in a function (Delphi 7)

I would like to write a function that checks if a certain letter is in a certain word.

That is the current function (sry for the german)

function woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
  if wort[i]=buchstabe then
    showmessage(INTtoSTR(i))
    //LB_ausgabe.items.add(INTtoSTR(i));
end;

The way it's written now the function actually works. It shows one or several messages with the position(s) of the letter searched for (the variable "buchstabe") in the word "wort". E.g. for wort=abctc and buchstabe=c it shows 3 and 5.

But if i would write it this way

function woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
  if wort[i]=buchstabe then
    LB_ausgabe.items.add(INTtoSTR(i));
end;

(remove the showmessage and make the ListBox thing actual code)

then I get the error

Undefined Identifier: 'LB_ausgabe'

This is the complete code of the Unit

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    E_kette: TEdit;
    E_buchstabe: TEdit;
    B_start: TButton;
    LB_ausgabe: TListBox;
    procedure B_startClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
  if wort[i]=buchstabe then
    showmessage(INTtoSTR(i))
    //LB_ausgabe.items.add(INTtoSTR(i));
end;

procedure TForm1.B_startClick(Sender: TObject);
begin
woistderbuchstabe (E_kette.text, E_buchstabe.text);
end;

end.

Pls try to be specific as I'm pretty clueless about Delphi.

Thanks in advance

Upvotes: 0

Views: 11349

Answers (2)

Ken White
Ken White

Reputation: 125747

In my opinion, if you might need to use this logic from somewhere else, you need to decouple it from your user interface. You can do this by changing your function to a procedure that accepts a more generic class to populate (like a plain old TStrings) as a parameter. As TStrings is a common base for TComboBox.Items, TListBox.Items, TMemo.Lines and is used in many other places, this seems like the most flexible way to accomplish what you want to do.

procedure woistderbuchstabe (List: TStrings; wort, buchstabe:String);
VAR i: Integer;
begin
for i := 1 to length(wort) do
  if wort[i] = buchstabe then
    List.Add(InttoStr(i));
end;

This allows you to use the procedure with your TListBox (call it with LB_ausgabe.items, a TMemo, using Memo1.Lines, a TComboBox with Combobox1.Items, a TRichEdit with RichEdit1.Lines, or a plain old TStringList directly with SL.

You can now call it from anywhere you want, such as a TForm.Button1Click(Sender: TObject), using ListBox1.Items, or a standalone method that creates and passes in a TStringList. It's not tied to a specific form, so it's more flexible and able to be reused elsewhere.

Upvotes: 1

GabrielF
GabrielF

Reputation: 2121

Function woistderbuchstabe is not a member of your class TForm1... so it doesn't have direct access to it's members unless you specify an instance. I suggest this fix:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    E_kette: TEdit;
    E_buchstabe: TEdit;
    B_start: TButton;
    LB_ausgabe: TListBox;
    procedure B_startClick(Sender: TObject);
  private
    { Private-Deklarationen }
    function woistderbuchstabe (wort, buchstabe:String):String;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function TForm1.woistderbuchstabe (wort, buchstabe:String):String;
VAR i: Integer;
begin
for i:=1 to length(wort) do
  if wort[i]=buchstabe then
    LB_ausgabe.items.add(INTtoSTR(i));
end;

procedure TForm1.B_startClick(Sender: TObject);
begin
woistderbuchstabe (E_kette.text, E_buchstabe.text);
end;

end.

But you can also just reference your Form1: TForm1 instance (global variable), in your function (I recommend you stick with the OO approach, though):

Form1.LB_ausgabe.items.add(INTtoSTR(i));

PS: Check Pos and PosEx functions too, as they are probably (I never benchmarked) faster solution, since they are asm implemented.

Upvotes: 1

Related Questions