Solved

Clear a record

Posted on 2001-07-17
12
200 Views
Last Modified: 2010-04-06
I am using Delphi 2 and am wondering if there is a better, cleaner way to clear a record structure.

example

Type TheRec = Record
               LastName  : String[100];
               FirstName : string[100];
               Deleted  : Boolean;
              end;

when i save the record i have noticed that some of the info from a previous record is still there too.

is there any way to clear the record contents without having to assign blank values to them?

also, rec.firstname:='' does not work.
0
Comment
Question by:mi6agent
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6292558
look at

fillchar

didn't have the correct syntax in mind, but should be like

fillchar(ARec,#0,SizeOf(ARec));

this should fill the rec with 0s

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6292565
correct syntax:

FillChar(Arec, sizeof(Arec), #0);

:-))

   
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 6292573
just to explain why
rec.firstname:='' does not work

this call just modifies the lengthindex of the string and sets it to 0, but the rest still resists in the memory-area of the record.

meikl ;-)
0
Industry Leaders: 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 13

Expert Comment

by:Epsylon
ID: 6292609
Or try:

ZeroMemory(@rec, sizeof(rec));
0
 

Author Comment

by:mi6agent
ID: 6292623
thanks meikl.  i knew there had to be a better way than the way i was doing it.

0
 

Author Comment

by:mi6agent
ID: 6292643
thanks epsylon seems there is always at least two ways to do something.

Meikl got in before you with the answer on this one so i shall award him the points - hope you see that as fair.
0
 
LVL 17

Expert Comment

by:geobul
ID: 6292645
Hi,

I strongly recommend clearing with assignment field by field. Create a function for that purpose and use it when necessary. The reason is that #0 (or any other character) is not always applicable for all possible fields in a record structure. You can have string field which is a pointer (not like string[100]) or a set field like:
Month: (Jan, Feb, Mar, Apr, May, Jun,
            Jul, Aug, Sep, Oct, Nov, Dec);
What does #0 mean for boolean field? True or false?

So:

function TheRecInit(var ARec: TheRec): integer;
begin
  result := 0;
  try
    ARec.LastName := '';
    ARec.FirstName := '';
    ARec.Deleted := false;
  except
    result := 1;
  end;
end;

Regards, Geo
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6292675
0 for boolean means false, geo,
but you've right that after clearing
a record should be init with its defaults

yes, mi6agent,
many ways are allways possible

thanks for the points :-)
glad to helped you again

meikl ;-)
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6292685
If you declare a record like

var rec: TheRec;

Delphi automatically initializes all bytes to zero.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6292706
thats true eps,
but only on startup,
if u use the var multiple times,
then may fragments resists

meikl ;-)
0
 
LVL 17

Expert Comment

by:geobul
ID: 6292776
Hi meikl,
What if the field was NotDeleted? Just kidding :)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6292834
as i said,
after clearing, the record should
be initialized with its defaults

o, you are just kidding :-))
0

Featured Post

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!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

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