Solved

Variable RecordTypes as parameter for procedures

Posted on 1998-11-08
2
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
ow earned 100 total points
ID: 1346123
 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
ID: 1346124
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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