Solved

Initialising a record

Posted on 2000-04-26
8
148 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now