Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Record structure at runtime

Status
Not open for further replies.

Tilen

Programmer
Apr 2, 2005
75
SI
Hi

how would I get a structure of a specific record type in runtime?

Let say I have a record

Type TRec = record
ID:integer;
Name:string;
Gender:char;
Active:boolean;
end;

var Clients:array of TRec;


How would I get a name, type and data of each element/attribute of Clients?

For example I would to do something like this:

for i:=0 to Clients.NoOfElements-1 do
begin
vName:=GetElementName(Clients,i);
vType:=GetElementType(Clients,i);
vData:=GetElementData(Clients,i);
...
end;


Google show some examples of using RTTI functions, like GetPropList, GetPropName... but these are all used for classes and their published properties, if I understand correctly.

What about array... is it possible?

Thanx a lot

Tilen
 
I dont know if this is what you mean but records are accessed with dot notation in pascal.

I would keep the record global i.e dont bother passing it into the function.

e.g.
Code:
function GetElementName(i: integer): string;
begin
 result := Clients[i].name;
end;

function GetElementID(i: integer): integer;
begin
 result := Clients[i].ID;
end;
Also I would not use 'char' to hold the gender field, define a simplae type to do this


Type TGender = (Male, Female, Other);
var Gender: TGender;

Code:
function GetGender(i: integer): TGender;
begin
 result := Clients[i].Gender;
end;

There is no siple way to get the number of records in your array, the way you have defined it
Code:
var  Clients: array of TRec;

Defines a dynamic array, you must use the 'setlength' function to allocate memory for this.

If you have a lot of data consider using the database components, if not and you now how much data there is likely to be then define a fixed size array and keep a check on how full it is.

e.g.
var Clients: array [0..1000] of TRec;





Steve [The sane]: Delphi a feersum engin indeed.
 
Thanx for the effort, but I know all this :)

I have lots of arrays with lots of attributes/elements, and I need to make a snapshot (save to disk - text,xml...) of the data, when the program is running.

I could write Save and Load procedure for each array/record, but it would take a lot of time and event then every change (add,remove, modify) in the record definitions would mean I would need to change the record's Save and Load procedures.

So I would like to have one general Save procedure which would save the records structure and data.

And that is why I need a 'general' approach to record's structure and data.

Not with Clients.ID but some generic code that would know how to access one by one each element, get it's type, name and data, for any record given.


Current process of my program:

1. Select source file with data
2. Import data from source
3. Process and do some calculation
4. Analyze data and show results

Points 2 and 3 together could take from 5s to 1 hour or more.

I need:

3.a. Make snapshot of processed data

So that next time (tomorrow, next week, month...) or maybe some of co-workers to be able to just load a snapshot, which should take just a fraction of how much points 2 and 3 is taking now.

So next run would be:

1. Select snapshot file
2. Load snapshot
3. Analyze data

I hope this is clearer of what I'm trying to do.

Tilen

 
Well in general, have you considered makeing the record into a type definition (OOP it)
and save the data to stream.
If you make it an object then you can use OOP techniques to implement your save procedures.
That way you can have a single interface, but there will be quite a lot hidden, so its not a simple option.







Steve [The sane]: Delphi a feersum engin indeed.
 
I'm not really sure I know what OOP techniques are...
 
I think he means to turn your record into an object complete with constructor and destructor. You can still reference your elements the same but you should have better access to the RTTI information.

It would also allow you to create a generic 'high level' object that has the .Save and .Load functionality you want to implement. Then you can derive your specific record types from that base object so they have access to the save/load functionality.

As an aside disconnected ADO recordsets can be saved to disk and loaded as required.

Regards,

Django
 
Thank you for suggestions. I just started looking at classes... I'll post my results.
 
Yup sorry been busy, but thats exactly what I meant.
Thanks Danjman.



Steve [The sane]: Delphi a feersum engin indeed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top