Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Clear a record

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
mi6agent
Asked:
mi6agent
  • 6
  • 2
  • 2
  • +1
1 Solution
 
kretzschmarCommented:
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
 
kretzschmarCommented:
correct syntax:

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

:-))

   
0
 
kretzschmarCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
EpsylonCommented:
Or try:

ZeroMemory(@rec, sizeof(rec));
0
 
mi6agentAuthor Commented:
thanks meikl.  i knew there had to be a better way than the way i was doing it.

0
 
mi6agentAuthor Commented:
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
 
geobulCommented:
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
 
kretzschmarCommented:
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
 
EpsylonCommented:
If you declare a record like

var rec: TheRec;

Delphi automatically initializes all bytes to zero.
0
 
kretzschmarCommented:
thats true eps,
but only on startup,
if u use the var multiple times,
then may fragments resists

meikl ;-)
0
 
geobulCommented:
Hi meikl,
What if the field was NotDeleted? Just kidding :)
0
 
kretzschmarCommented:
as i said,
after clearing, the record should
be initialized with its defaults

o, you are just kidding :-))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 6
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now