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
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.