Solved

Initialising a record

Posted on 2000-04-26
8
152 Views
Last Modified: 2010-04-04
I'm trying to tidy up my code. So rather than having a procedure with 5 parameters I've created a data structure (i.e. record) to represent these paremeters.  The record gets passed as a single parameter to the procedure...

TSomeData = record
 Command : string;
 Return  : string;
 ReturnChr : Char;
 ReturnChrCount : integer;
 ReturnTimeOut  : LongWord;
end;

proc DoStuff(var AData : TSomeData);
begin
  //do stuff
end;

proc CallDoStuff;
var
  AData : TSomeData;
begin
  with AData do
  begin
    Command := 'abc';
    ReturnChr := #13;
    ReturnChrCount := 1;
    ReturnTimeOut  := 100;
  end;
  DoStuff(AData);
end;

My problem is that I want some of the fields to be intitialised to some default value if not set by the calling function.  How can I do this?

Andrew
0
Comment
Question by:andrewjackson
8 Comments
 
LVL 10

Expert Comment

by:Lischke
ID: 2750868
Hi Andrew,

I think you have no other choice than to explicitely initialize the record with your default values. Just as you did in CallDoStuff. With D5's default parameter this would be easier...

To ease the work with non-zero default values you can define a constant with the values and do a simple assignment of the structure to be passed to DoStuff like:

const
 DefaultData: TSomeData = (
  Command: 'NULL';
  Return: '';
  ReturnChr: #13;
  ReturnChrCount: 1;
  ReturnTimeOut: 100;  
 );

and:

proc CallDoStuff;
var
  AData : TSomeData;
begin
  AData := DefaultData;
  with AData do
  begin
    Command := 'abc';
  end;
  DoStuff(AData);
end;

Ciao, Mike
0
 
LVL 1

Expert Comment

by:AttarSoftware
ID: 2750874
Not what you want, but

var
  AData : TSomeData;
begin
  FillChar( AData, sizeof( TSomeData ), #0 ) ;
end ;

Would initialise everything to zero...

Tim.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2750875
Not possible unless you reserve zero values to test if a record field has been set or not. I guess you don't want this...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:msedi
ID: 2750886
Hello,

I think Lischkes' answer is the best because with AttarSoftwares' answer there is not really a wanted default value. Just 0 is not a default value if not wanted. Nevertheless this version of FillChar could also me achieved with ZeroMemory.

Thanks, Martin
0
 

Author Comment

by:andrewjackson
ID: 2750915
Thanks for your replies.  Unfortunately I don't think any of them will work in my case.  

I had thought of using an class/object instead of a record since I could control field initilaisation in the constructor but that means the caller function having to create and destroy an object just to pass some parameters.

Andrew
0
 
LVL 10

Accepted Solution

by:
Lischke earned 50 total points
ID: 2750938
Well, what I gave you is what you can do (clearing to zero is also an option, but not really assigning default values as already mentioned).

So your option is to to use the default record, discard the record-as-parameter approach and use Delphi's default parameters or to accept that the answer to your question is no. Either one is a valid answer, please consider that.

Ciao, Mike
0
 
LVL 1

Expert Comment

by:veeramani
ID: 2750967
Hi Andrew,
          here is the code for you.


TSomeData = record
 Command : string;
 Return  : string;
 ReturnChr : Char;
 ReturnChrCount : integer;
 ReturnTimeOut  : LongWord;
end;

proc DoStuff(var AData : TSomeData);
begin
  //do stuff
end;

proc InitializeToDefault(var AData : TSomeData);
begin
  with AData do //Assign the default
                //values you wish
  begin
    Command := '';
    ReturnChr := #0;
    ReturnChrCount := 0;
    ReturnTimeOut  := 0;
  end;
end;


proc CallDoStuff;
var
  AData : TSomeData;
begin
InitializeToDefault(AData);
  with AData do
  begin
    Command := 'abc';
    ReturnChr := #13;
    ReturnChrCount := 1;
    ReturnTimeOut  := 100;
  end;
  DoStuff(AData);
end;

Regards,

Veeramani
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2751007
???
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 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