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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through record 1

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I have created a record to store information in response to questions. Is there a way that I can loop through each attribute of the record and see if there's a value?

for instance, if I have:

Code:
FInterview : Record
  Name : string;
  InterviewDate : Tdate;
  Interviewer : string;
...
CurrentInterview := FInterview;
...
  CurrentInterview.Name := eName.Text
  CurrentInterview.InterviewDate := Today();
...

is there a way to loop through the record and see that Name and InterviewDate are completed, but Interviewer is not?

thanks for any insight!

Leslie
 
Presumably your example should have been something like
Code:
type
  FInterview = record
    Name: string;
    InterviewDate: TDate;
    Interviewer: string;
  end;
...
var
  CurrentInterview: FInterview;
...
begin
  CurrentInterview.Name := eName.Text
  CurrentInterview.InterviewDate := Today();
...
You could do something like this:
Code:
type
  TAttribute = ( aName, aInterviewDate, aInterviewer );

  FInterview = record
    attribute: array [ TAttribute ] of string;
  end;

var
  interview: FInterview;

procedure TForm1.Button1Click(Sender: TObject);
var
  a: TAttribute;
begin
  interview.attribute[aName] := eName.Text;
  interview.attribute[aInterviewDate] := DateToStr( Today() );
  for a := Low(TAttribute) to High(TAttribute) do
    if interview.attribute[a] = '' then
// Do something
    ;
end;
But to be honest, I am not really sure what you are trying to achieve.

Andrew
Hampshire, UK
 
You clearly want to perform some checks on the data stored. Therefore, I would suggest using a class instead of a record, then the checking can be built into the class itself.
Code:
TInterview = class
private
  fName: String;
  fInterviewDate: TDate;
  fInterviewer: String;
public 
  property Name: String read fName write fName;
  property InterviewDate: TDate read fInterviewDate write fInterviewDate;
  property Interviewer: String read fInterviewer write fInterviewer;
  function AllDataEntered: Boolean;
end;

...

function TInterview.AllDataEntered: Boolean;
begin
  Result := (fName <> '') and 
            (fInterviewer <> '') and 
            (fInterviewDate <> ...);
end;
Obviously, you don't have to use properties and you will need to compare the date to something.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Thanks to both of you for your input!

Clive, thank you for showing me an example of a class that actually makes sense to me!

Thanks! This has been extremely helpful.

Leslie

 
Often, I start using records but then need to act upon the component parts. This is best done by an object which encapsulates both data and behaviour of that data.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top