Solved

Initialising a record

Posted on 2000-04-26
8
150 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Send message from delphi to whatsapp, is it possible? 3 1,422
Delphi - replicating a form 8 73
Firemonkey webbrowser scrollbars ? 1 38
Dev express lookupcombo 3 26
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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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