Solved

Variable RecordTypes as parameter for procedures

Posted on 1998-11-08
2
202 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

624 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