Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Variable RecordTypes as parameter for procedures

Posted on 1998-11-08
2
Medium Priority
?
209 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 300 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

773 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