Solved

Initialising a record

Posted on 2000-04-26
8
153 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
[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
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…

734 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