Board index » delphi » Function return is not returned properly?

Function return is not returned properly?

Hi!

I just wrote a little recursive function that replaces all occurrences of a
string in a string. It works properly but if I want the return to be displayed
it displays a blank line...

If you need such a function feel free to use it once you get it to work...

What's wrong?

This is the code (with comments):

##############

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    function ReplaceItAll(ReplaceText, ReplaceWhat, ReplaceWith : String) :
String;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function TForm1.ReplaceItAll(ReplaceText, ReplaceWhat, ReplaceWith : String) :
String;
var RepMe, PassRepText : String;
    sBegin, sEnd : String;
    iPos, iLength, iTotalLength : Integer;

begin
iPos := Pos(ReplaceWhat, ReplaceText);
     // ^Finds the occurrence.
PassRepText := ReplaceText;
if iPos > 0 then // checks if there needs to be any more replacements.
   begin
   iTotalLength := Length(ReplaceText);
   iLength := Length(ReplaceWhat);
   sBegin := Copy(ReplaceText, 0, iPos - 1);
          // ^The part of the text before the search word.
   sEnd := Copy(ReplaceText, iPos + iLength, iTotalLength - (iPos + iLength) +
1);
          // ^The part of the text after the search word.
   PassRepText := sBegin + ReplaceWith + sEnd;
          // ^The text with the current replacement included.
   showmessage(PassRepText);
   ReplaceItAll(PassRepText, ReplaceWhat, ReplaceWith);
          // ^Replace the next occurrence in the now replaced text/
   end
   else
   begin
   showmessage(PassRepText);
   ReplaceItAll := PassRepText;
          // ^This should return the fully replaced text...
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(Replaceitall(Memo1.Lines.Text, Edit1.Text, Edit2.Text));
          // ^But it doesn't seem to do so...
end;

end.

--
Jan van der Crabben
 Cre@tive Webdesign
  webmas...@henstedt-ulzburg.net
  http://www.henstedt-ulzburg.net/creative/

 

Re:Function return is not returned properly?


Hi Jan,

You are the result forgotten! (twice)

function TForm1.ReplaceItAll(ReplaceText, ReplaceWhat, ReplaceWith : String): String;
var  iPos: Integer;
begin
  iPos  := Pos(ReplaceWhat, ReplaceText);
  if iPos > 0 then begin
     Delete(ReplaceText, iPos, length(ReplaceWhat));
     Insert(ReplaceWith, ReplaceText, iPos);
  end;
  result:= ReplaceText;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(Memo1.Lines.Text:=
            Replaceitall(Memo1.Lines.Text, Edit1.Text, Edit2.Text));
end;

Rudy
====================================
Jan van der Crabben schreef:

Quote

> Hi!

> I just wrote a little recursive function that replaces all occurrences of a
> string in a string. It works properly but if I want the return to be displayed
> it displays a blank line...

> If you need such a function feel free to use it once you get it to work...

> What's wrong?

> This is the code (with comments):

> ##############

> unit Unit1;

> interface

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

> type
>   TForm1 = class(TForm)
>     Memo1: TMemo;
>     Edit1: TEdit;
>     Button1: TButton;
>     Edit2: TEdit;
>     function ReplaceItAll(ReplaceText, ReplaceWhat, ReplaceWith : String) :
> String;
>     procedure Button1Click(Sender: TObject);
>   private
>     { Private declarations }
>   public
>     { Public declarations }
>   end;

> var
>   Form1: TForm1;

> implementation

> {$R *.DFM}

> function TForm1.ReplaceItAll(ReplaceText, ReplaceWhat, ReplaceWith : String) :
> String;
> var RepMe, PassRepText : String;
>     sBegin, sEnd : String;
>     iPos, iLength, iTotalLength : Integer;

> begin
> iPos := Pos(ReplaceWhat, ReplaceText);
>      // ^Finds the occurrence.
> PassRepText := ReplaceText;
> if iPos > 0 then // checks if there needs to be any more replacements.
>    begin
>    iTotalLength := Length(ReplaceText);
>    iLength := Length(ReplaceWhat);
>    sBegin := Copy(ReplaceText, 0, iPos - 1);
>           // ^The part of the text before the search word.
>    sEnd := Copy(ReplaceText, iPos + iLength, iTotalLength - (iPos + iLength) +
> 1);
>           // ^The part of the text after the search word.
>    PassRepText := sBegin + ReplaceWith + sEnd;
>           // ^The text with the current replacement included.
>    showmessage(PassRepText);
>    ReplaceItAll(PassRepText, ReplaceWhat, ReplaceWith);
>           // ^Replace the next occurrence in the now replaced text/
>    end
>    else
>    begin
>    showmessage(PassRepText);
>    ReplaceItAll := PassRepText;
>           // ^This should return the fully replaced text...
>    end;
> end;

> procedure TForm1.Button1Click(Sender: TObject);
> begin
> showmessage(Replaceitall(Memo1.Lines.Text, Edit1.Text, Edit2.Text));
>           // ^But it doesn't seem to do so...
> end;

> end.

> --
> Jan van der Crabben
>  Cre@tive Webdesign
>   webmas...@henstedt-ulzburg.net
>   http://www.henstedt-ulzburg.net/creative/

Re:Function return is not returned properly?


Dear, Jan van der Crabben

Two thing should be corrected.

    One, 'ReplaceItAll' is a function.  Hence, it need a result value.

    Second, what does 'Repme' variable do in this function?

Here is the revised function.

function TForm1.ReplaceItAll(ReplaceText, ReplaceWhat, ReplaceWith : String)
: String;
var PassRepText : String;
     sBegin, sEnd : String;
     iPos, iLength, iTotalLength : Integer;

 begin
    iPos := Pos(ReplaceWhat, ReplaceText);
    // ^Finds the occurrence.
    PassRepText := ReplaceText;
    if iPos > 0 then // checks if there needs to be any more replacements.
        begin
        iTotalLength := Length(ReplaceText);
        iLength := Length(ReplaceWhat);
        sBegin := Copy(ReplaceText, 0, iPos - 1);
            // ^The part of the text before the search word.
        sEnd := Copy(ReplaceText, iPos + iLength, iTotalLength - (iPos +
iLength) + 1);
            // ^The part of the text after the search word.
        PassRepText := sBegin + ReplaceWith + sEnd;
            // ^The text with the current replacement included.
        showmessage(PassRepText);
        Result := ReplaceItAll(PassRepText, ReplaceWhat, ReplaceWith);
            // ^Replace the next occurrence in the now replaced text/
         end
     else
        begin
            showmessage(PassRepText);
            Result := PassRepText;
                // ^This should return the fully replaced text...
        end;
end;

This might work.
Good luck.

Earl

Jan van der Crabben <webmas...@henstedt-ulzburg.net> wrote in message
news:3705610A.F371C3E6@henstedt-ulzburg.net...

Quote
> Hi!

> I just wrote a little recursive function that replaces all occurrences of
a
> string in a string. It works properly but if I want the return to be
displayed
> it displays a blank line...

> If you need such a function feel free to use it once you get it to work...

> What's wrong?

> This is the code (with comments):

> ##############

> unit Unit1;

> interface

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

> type
>   TForm1 = class(TForm)
>     Memo1: TMemo;
>     Edit1: TEdit;
>     Button1: TButton;
>     Edit2: TEdit;
>     function ReplaceItAll(ReplaceText, ReplaceWhat, ReplaceWith : String)
:
> String;
>     procedure Button1Click(Sender: TObject);
>   private
>     { Private declarations }
>   public
>     { Public declarations }
>   end;

> var
>   Form1: TForm1;

> implementation

> {$R *.DFM}

> function TForm1.ReplaceItAll(ReplaceText, ReplaceWhat, ReplaceWith :
String) :
> String;
> var RepMe, PassRepText : String;
>     sBegin, sEnd : String;
>     iPos, iLength, iTotalLength : Integer;

> begin
> iPos := Pos(ReplaceWhat, ReplaceText);
>      // ^Finds the occurrence.
> PassRepText := ReplaceText;
> if iPos > 0 then // checks if there needs to be any more replacements.
>    begin
>    iTotalLength := Length(ReplaceText);
>    iLength := Length(ReplaceWhat);
>    sBegin := Copy(ReplaceText, 0, iPos - 1);
>           // ^The part of the text before the search word.
>    sEnd := Copy(ReplaceText, iPos + iLength, iTotalLength - (iPos +
iLength) +
> 1);
>           // ^The part of the text after the search word.
>    PassRepText := sBegin + ReplaceWith + sEnd;
>           // ^The text with the current replacement included.
>    showmessage(PassRepText);
>    ReplaceItAll(PassRepText, ReplaceWhat, ReplaceWith);
>           // ^Replace the next occurrence in the now replaced text/
>    end
>    else
>    begin
>    showmessage(PassRepText);
>    ReplaceItAll := PassRepText;
>           // ^This should return the fully replaced text...
>    end;
> end;

> procedure TForm1.Button1Click(Sender: TObject);
> begin
> showmessage(Replaceitall(Memo1.Lines.Text, Edit1.Text, Edit2.Text));
>           // ^But it doesn't seem to do so...
> end;

> end.

> --
> Jan van der Crabben
>  Cre@tive Webdesign
>   webmas...@henstedt-ulzburg.net
>   http://www.henstedt-ulzburg.net/creative/

Re:Function return is not returned properly?


Im Artikel <3705610A.F371C...@henstedt-ulzburg.net>, Jan van der Crabben
<webmas...@henstedt-ulzburg.net> schreibt:

Quote
>   ReplaceItAll(PassRepText, ReplaceWhat, ReplaceWith);

The subroutine receives a copy of PassRepText, therefore changes are not
reflected in the calling code. Try:

Result := ReplaceItAll(PassRepText, ReplaceWhat, ReplaceWith);

DoDi

Other Threads