Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Initialize a pascal record

2,932 views
Skip to first unread message

Steffan Bansemer

unread,
Jan 14, 2001, 9:25:04 AM1/14/01
to
Hi,

if I have many record types like

type RecordA = record
Name: String;
Street: String;
Age: Integer;
end;

type RecordB = record
Index: Integer;
Caption: String;
Complete: Boolean;
end;

...

is there a simple way to initialize them?
Strings with '', Integers with 0, Boolean with FALSE etc.?
I would wish there would be a procedure like InitRecord(Record: Variant);
that does this. But there won´t or?
Some Record have near or more than 20 sub declarations and it´s very bad
to write procedures to initialize each record.

Thanks.

Iroel Perez

unread,
Jan 14, 2001, 10:02:41 AM1/14/01
to
Did you try FillChar(R, sizeof(R), 0)

"Steffan Bansemer" <inpris...@bansemer.de> wrote in message
news:93scub$hm...@bornews.inprise.com...

Steffan Bansemer

unread,
Jan 14, 2001, 11:02:23 AM1/14/01
to
I found two solutions

1)
// to clear memory of that record
Finalize(RecordA);
FillChar(RecordA, SizeOf(RecordA), 0);

2)
// to get an empty record


type RecordA = record
Name: String;
Street: String;
Age: Integer;
end;

const _RecordA: RecordA =
(Name: ''; Street: ''; Age: 0);

But what will be the most safe method and
what will be the smartest method?


Ralph Friedman (TeamB)

unread,
Jan 14, 2001, 12:39:29 PM1/14/01
to
Steffan,
you wrote in message <93siku$hm...@bornews.inprise.com>:

if you use a Pointer to your record and New, the New procedure takes care of
initializing the record. If you want to initialize with data the (pseudo) const
declaration is the only means to do it. You might also want to take a look at
the ZeroMemory API.

--
Regards
Ralph (TeamB)
===

John Herbster

unread,
Jan 14, 2001, 6:49:53 PM1/14/01
to
Ralph, I just discovered the Initialize() procedure in the D5 help.
Apparently it may be one of the primitives used by the New()
function. Have you ever found a use for it? Rgds, JohnH


Carl Reynolds

unread,
Jan 14, 2001, 9:13:15 PM1/14/01
to
"Steffan Bansemer" <inpris...@bansemer.de> wrote in message
news:93scub$hm...@bornews.inprise.com...
> if I have many record types like
>
> type RecordA = record
> Name: String;
> Street: String;
> Age: Integer;
> end;
>
> is there a simple way to initialize them?

Steffan, is it possible for you to use objects instead of records?
Object fields are initialised to zero when the object is created, and
you can add constructors for more sophisticated initialisation - as well
as using all the benefits that classes provide.

type
TRecordA = class
private


Name: String;
Street: String;
Age: Integer;

public
constructor Create(AName: string; AStreet: string = ''); virtual;
end;

constructor TRecordA.Create(AName: string; AStreet: string = '');
begin
// AName = '', AStreet = '', AAge = 0;
end;

Carl Reynolds

unread,
Jan 14, 2001, 9:18:08 PM1/14/01
to
"Steffan Bansemer" <inpris...@bansemer.de> wrote in message
news:93scub$hm...@bornews.inprise.com...
> if I have many record types like
>
> type RecordA = record
> Name: String;
> Street: String;
> Age: Integer;
> end;
>
> is there a simple way to initialize them?

Steffan, is it possible for you to use objects instead of records?


Object fields are initialised to zero when the object is created, and
you can add constructors for more sophisticated initialisation - as well
as using all the benefits that classes provide.

Cheers,
Carl


Ralph Friedman (TeamB)

unread,
Jan 15, 2001, 3:53:56 AM1/15/01
to
John,
you wrote in message <3a623aa1$1_1@dnews>:

> Ralph, I just discovered the Initialize() procedure in the D5 help.
> Apparently it may be one of the primitives used by the New()
> function. Have you ever found a use for it? Rgds, JohnH
>

I've just become "re-acquainted" with it myself<g>. It does appear that it
would work if you use GetMem to get records that need initializing. But then,
New does that for you... There may be some practical uses for it when working
with variants, however.

0 new messages