You are not logged in.
Pages: 1
Hello friends,
Is there any "magic" method that sets the initial values of a record?
Example:
TMyRecord = record
MyInt : Integer;
MyDate : TDateTime;
end;
A method that set MyInt and MyDate to 0.
=========
Another subject (however related as a solution to this is to use classes)
@ab, could you consider moving ObjectLoadJSON to the SynCommons.pas unit?
This method is in Mormot.pas, but due to its generalization I believe it could be in SynCommons to avoid dependence on Mormot.pas.
Especially because ObjectToJSON is in SynCommons
Last edited by macfly (2020-01-10 19:45:28)
Offline
If your compiler supports it, you can do:
myVar := Default(TMyRecord)
Offline
Thanks for reply.
This works, but not in Delphi 7.
Se this case:
TMyRecord = record
Name : String;
...
Date : TDatetime;
end;
var
MR : TMyRecord;
MR.Name := 'Jhon';
RecordSaveJSON(MR, TypeInfo(TMyRecord )); //<<
This raise a invalid floating point operation because Date whas not setted.
Then I need to create a function or constant to init the record.
A generic (cross-compiler) method would be of great help.
Offline
ObjectToJSON() needs class RTTI and TSQLRecord, which are not available in SynCommons, but are defined in mORMot.pas.
Thanks ab,
I see now that if i remove mORMot.pas, ObjectToJSON returns null.
Offline
Please check RecordZero() from https://synopse.info/fossil/info/3fb0a0171a
Offline
Wow, you are fast!
Checking now.
Thanks ab.
PS: Maybe this new method must be in documentation as a recommendation to avoid erros in serilization.
A record with much/nested fields, are easy to bypass the initialization.
Edit: Tested RecordZero() implementation, and Working.
Last edited by macfly (2020-01-10 19:45:02)
Offline
Pages: 1