Forum > General

passing textfile into procedure

(1/2) > >>

Hi, I am new to Lazarus, but I have following problem with passing TextFile into my procedure. Here is code:

--- Code: ---unit TSkaut;

{$mode objfpc}{$H+}


  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs;

type Skaut = class
    constructor Create(tf : TextFile);


constructor Skaut.Create(tf : TextFile);



--- End code ---
and I get following error:

Error: File types must be var parameters

Any idea what to do with it ? :(

When I replace TextFile with Integer for example, it works ...



The compiler tells you what to do... :-)

constructor Create(var tf : TextFile);

hth - regards benny

Thank you very much.

Can you explain why ? Why can I use it 'the normal way' with integer but it requires it done this way with TextFile ?


Well yes... here goes:

It's called passing parameters by value or by reference.

by Value means you pass a parameter like this
--- Code: ---function AddTen(anInt: integer):integer;
--- End code ---
the function/procedure then gets its own local variable to work on, ie. it doesn't change the original you passed to it.
You pass the value of it

by reference means you pass a parameter like this
--- Code: ---procedure AddTen(VAR anInt: integer)
--- End code ---
the function/procedure then gets a reference/pointer to the original variable and works on that, ie. it changes the original you passed to it.
You pass a reference (a pointer) to the original variable

The TextFile type is a handle (kind of pointer) to the actual file on disk, it is itself a reference ;)
So to be able to work on the file on disk, you need to pass the handle to it as a reference variable to your function.
--- Code: ---constructor Create(var tf : TextFile);
--- End code ---

Pheeeewwww.... that was verbose  %) I hope it makes sense, what is going on behind the scenes...

Keep hacking, pascal is a great language.
Regards Benny

Thanks mate. I should have told you that I have lot of experiences with C++ and Java, so passing by reference is all I need to know.

Your help is much appreciated!


[0] Message Index

[#] Next page

Go to full version