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.
"Steffan Bansemer" <inpris...@bansemer.de> wrote in message
news:93scub$hm...@bornews.inprise.com...
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?
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)
===
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;
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
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.