Solved

Clear a record

Posted on 2001-07-17
12
196 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now