Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Clear a record

Posted on 2001-07-17
12
Medium Priority
?
207 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

824 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