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
Solved

Initialising a record

Posted on 2000-04-26
8
151 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
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.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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