hi varg300,
I think you may still be still a little confused how the variant record works.
Suppose you define this:
type
SomeRecord= record
case boolean of
true: (
// your existing record members come here
);
false: (
RecordVersion:dword;
VersionDesc: String[31];
VersionDate: tDateTime;
);
end;
SomeRecord will have length of the longer of all the fields in the True clause or all the fields in the false clause; You also are not restricted to just 2 record formats.
You could code:
type
SomeRecord= record
case Integer of of
0: (
RecordVersion:dword;
VersionDesc: String[31];
VersionDate: tDateTime;
);
1: (
// your existing record members come here
);
2: (
// your modified record members come here
);
3: (
// your 'modified' modified record members come here
);
etc.
end;
You never need to set True or False ( or 1, 2. 3, etc) anywhere in your program.
The problem you will have though is that the record length will (usually) change with each modification and so you will have problems accessing different versions of the file.
A technique to avoid this is to simply add a 'Reserved for future use field' as the last field in the record:
type
SomeRecord= record
case boolean of
true: (
// your existing record members come here
// and then
Reserved: array[0..7] of LongInt; // Reserve 32 bytes for future use
);
false: (
RecordVersion:dword;
VersionDesc: String[31];
VersionDate: tDateTime;
);
end;
Now, as you add new fields to the record, simply reduce the size of the Reserved array.
A few notes on the above:
1) You can ensure that your 'new' fields are initialised in your old file by:
FillChar(mySomeRecord, SizeOf(SomeRecord), #0);
before you create the first record. Doing this will ensure that new binary fields will be zero and new strings will be ''
2) Clearly this wastes disk space, but considering that I used this technique 40 years ago when disk space was at a premium, with todays terabyte disks, the wastage should be minimal.
3) Note to Reserve bytes for future use I used an array of LongInt. I could have used an array of char, but this introduces a new problem Fields in a record may have some 'padding' between them. For example, a LongInt field will be aligned on a 4 byte boundary. (google Free Pascal PackedRecords for more info).
A useful technique here is to try to keep the record 4 byte aligned. e.g. if you have a field that is string[30], make it string[31] so that it is a multiple of 4 bytes in length.
***********
If you are prepared to learn a new file structure, then the FileStrean mentioned above is certainly one approach, but there is another that I think is even better:
I am just learning SQL (specifically SQLITE) and it is amazing. To achieve what you want you do not even need to worry about a FileVersionNumber. In the future, when you wish to add another field to your record you simply ADD COLUMN and the new fields in your existing file can either be set to NULL or given a DEFAULT value. Your modified program will still run on your old data file.
Hope this helps