Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Function implementation (or so i thought)

Status
Not open for further replies.

RobPouwelse

Technical User
Nov 19, 2001
77
NL
I'm back.. :):) and with a new error (for me)..

[Error] Unit1.pas(24): Unsatisfied forward or external declaration: 'TForm1.Func_from'

What am i doing wrong here??
I know it's something with Func_from (it's placement?)
but i already tried placing it in {public decleration} or removing it... didn't work :(

=================================
unit Unit1;

interface

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

type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
Bestand1: TMenuItem;
OpenTextbestand1: TMenuItem;
Opslaan1: TMenuItem;
Afsluiten1: TMenuItem;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
procedure Afsluiten1Click(Sender: TObject);
procedure OpenTextbestand1Click(Sender: TObject);
function Func_from (Str1, Str2 :string):string ;


private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Afsluiten1Click(Sender: TObject);
begin
application.terminate();
end;

procedure TForm1.OpenTextbestand1Click(Sender: TObject);
var
F: TextFile;
Textline, Str_from : string;

begin
OpenDialog1.Options := [ofAllowMultiSelect, ofFileMustExist];
OpenDialog1.Filter := 'Text files (*.txt)|*.txt|All files (*.*)|*.*';
OpenDialog1.FilterIndex := 2;

if OpenDialog1.Execute then
with OpenDialog1.Files do
begin
AssignFile(F,OpenDialog1.Filename);
Reset(F);
while not Eof(F) do
begin
Readln(F, Textline);
Str_from := Func_from('from', Textline);

if Str_from='' then
else Memo1.Lines.Append(Str_from);
end;
CloseFile(F);
end;
end;

function Func_from (Str1, Str2 :string):string ;
var index, StrLength : integer;

begin
index := pos( Str1, Str2 ) + 5;
if index <> 0 then
begin
StrLength := pos( '>, size', Str2 ) - index;
Str1 := copy( Str2, index+1, strLength-1 );
end;
end;

end.
=========================
 
Hi
You must add the prefix TForm1. to your function header
e.g. function TForm1.Func_from (Str1, Str2 :string):string ;

as the function declaration is part of the type declaration for TForm1

Hope this helps
Steve
 
Also leave the declaration/prototype as it is just apply the prefix to the Header.

Steve.
 
well, i fiddled around, and i found out that if i didn't put the prototype in the type section (but below it) that it would work... but then again i don't get any values ... could it be that i don't get a return value cause of this???

tnx.
Rob Pouwelse
 
I tried your solution, but still i don't get a return value.. (i know that wasn't my first question, but that one is already solved)
 
To make a function return a value you must assign the output to either.
1. The 'Result' variable (this is predefined in Delphi).
e.g. result := Str1;
or
2. The name of the function (The old 'Pascal' method)
e.g. Func_from := Str1

The above assumes that you want to return Str1.

You will encounter problems with none typed functions if you try to access objects that are in a type

e.g. Memo1 or opendialog1.

Hope this helps
Steve..
 
Also, if you want the value you are putting in Str1 to &quot;stick&quot;, it needs to be a var parameter -

function Func_from (var Str1, Str2:string):string ;

It typically isn't considered good coding practice to have both var parameters and a return value (result), though. Another way you could do it is with a procedure, and not use a return value at all.

procedure Func_form (var Str1, Str2: string);

TealWren
 
Thank u all.. You really helped me out again :)
I've learned a lot from this forum, although most of it i should've already know :)

For myself i'll type everything i learned here (if i missed something or if i misinterpreted anything, just yell =] )

First of all, to use Memo's and Label's (objects) in procedures, i must have declared it as part of TForm (simply put, i must add TForm1. in the header)
Secondly, if i have a function which should return a value (otherwise i should use a procedure) i must assign the output to either Result or the functions Name (example: Result:=str1; .. Func_from:=str1;)

and finally, (this i wouldn't have found out on my own) That it's considerd Bad coding if you have both var parameters and a return value. (although i must admit i dont really know why?).

Thanks guys, i really learned allot..
 

and finally, (this i wouldn't have found out on my own) That it's considerd Bad coding if you have both var parameters and a return value. (although i must admit i dont really know why?)


I'm not really sure why either, so you could probably disregard that. :) The best explanation I can give is that a function is supposed to return one value, and having it return multiple values usually begs the question of why you didn't just make it a procedure in the first place. Doesn't mean I haven't done it before though. :) TealWren
 
Hi
As for functions not taking var paramaters have a look at file managment routines.

SelectDirectory(var directory string; Options: TselectdirectoryOptions; Helpctx: Longint):Boolean;

and I expect there are more examples..
So if Borland can do It so can we.
Lets face it this is not in the same leauge as using 'goto'

The use of a boolean return value to indicate the validity of an action on a variable seems Ok to me.

Steve.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top