Solved

Clear a record

Posted on 2001-07-17
12
198 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
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 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

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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