Solved

Variable RecordTypes as parameter for procedures

Posted on 1998-11-08
2
171 Views
Last Modified: 2010-04-04
Is it anyway possible to have different/variable RecordTypes as input parameter for procedures?

Lets say I wanted to make a standard procedure for reading/writing records to 'files of recordtypes', is that possible like this or is there an easier way?
0
Comment
Question by:smitje
2 Comments
 
LVL 1

Accepted Solution

by:
ow earned 100 total points
Comment Utility
 Hi smitje,

it is possible, to use variable record types as input parameter, but as you normaly need to know the size of the records, this must be delivered too.
Here is an example:

function WriteData (var F :file; var Data; Count :integer) :boolean;
  var
    AmtTransferred :integer;
  begin
  BlockWrite(F, Data, Count, AmtTransferred);
  Result := (AmtTransferred = Count);
  end;

procedure CreateDataFile1;
  type
    tDataRec = record
      N :integer;
      S :string[5];
      end;
  var
    F :file;
    DataRec :tDataRec;
  begin
  AssignFile(F, 'test.dat');
  Rewrite(F, 1);
  DataRec.N := 1;
  DataRec.S := 'text';
  if WriteData(F, DataRec, SizeOf(DataRec)) then
    WriteLn('Ok')
  else
    WriteLn('Error');
  CloseFile(F);
  end;

I would prefer streams, to do this work:

procedure CreateData2;
  type
    tDataRec = record
      N :integer;
      S :string[5];
      end;
  var
    FileStream :tFileStream;
    DataRec :tDataRec;
  begin
  FileStream := tFileStream.Create('test.dat', fmCreate);
  DataRec.N := 1;
  DataRec.S := 'text';
  if (FileStream.Write(DataRec, SizeOf(DataRec)) = SizeOf(DataRec)) then
    WriteLn('Ok')
  else
    WriteLn('Error');
  FileStream.Free;
  end;


Regards
  ow
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
Hello Y'all...

This couple of lines...

if (FileStream.Write(DataRec, SizeOf(DataRec)) = SizeOf(DataRec)) then
       WriteLn('Ok')
     else
       WriteLn('Error');

should be as follows,,,,

if (FileStream.Write(DataRec, SizeOf(DataRec)) = SizeOf(DataRec)) then
       WriteLn(F, 'Ok')
     else
       WriteLn(F, 'Error');

Cheers,
Viktor
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now