Jump to content
Sign in to follow this  
AndrewHoward

Accept delimited & return an object

Recommended Posts

Hello,
I am receiving data about different holidays in a delimited string e.g:
Booking number, flight details, package type, etc...

 

There are serveral holiday types e.g
Beach holidays
Ski holidays
All-inclusive
City breaks

 

Most of the holiday types have the same fields but there are some differences, e.g. some holidays don't have fight details.

 

I would like to create a function to which I pass the delimited string & it returns the object which represents the booked holiday:
Booking number, flight details, package type
Booking number, cruise details, package type, pick-up point

 

Which is the best to achieve this ?

 

I can create a record which holds all possible fields & return that as a result.  Is there a better way?

Thank you.

Share this post


Link to post

I use records like this and it's really easy to add more fields, because you simply add to the record definition.

You just need to make sure you initialize result, Result := Default(TRecord), and all record fields are initialized no matter how many are defined.

 

I define a class when parsing need more complex rules or goes beyond single function. Then you have all the flexibility of full class.

Share this post


Link to post
Guest
On 11/2/2020 at 10:19 PM, AndrewHoward said:

Hello,
I am receiving data about different holidays in a delimited string e.g:
Booking number, flight details, package type, etc...

...

Which is the best to achieve this ?

I can create a record which holds all possible fields & return that as a result.  Is there a better way?

Thank you.

some like this:

 

implementation

{$R *.dfm}

type
  TMyRec = record
    ID: integer;
    Holliday: string;
    HolliDate: TDate;
    HolliHour: TTime;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  lMyRecords: TArray<TMyRec>;
  i         : integer;
begin
  SetLength(lMyRecords, 3);
  //
  lMyRecords[0].ID        := 1;
  lMyRecords[0].Holliday  := 'Holliday 1';
  lMyRecords[0].HolliDate := Date + 10;
  lMyRecords[0].HolliHour := Time + 0.4;
  //
  lMyRecords[1].ID        := 2;
  lMyRecords[1].Holliday  := 'Holliday 2';
  lMyRecords[1].HolliDate := Date + 40;
  // lMyRecords[1].HolliHour := Time + 1.25; // time empty = 00:00:00
  //
  lMyRecords[2].ID       := 3;
  lMyRecords[2].Holliday := 'Holliday 3';
  // lMyRecords[2].HolliDate := Date + 45; // date empty = 30/12/1899
  lMyRecords[2].HolliHour := Time + 1;
  //
  for i := low(lMyRecords) to high(lMyRecords) do
    ShowMessage(                          { }
      Format('%d; %s; %s; %s', [          { }
      lMyRecords[i].ID,                   { }
      lMyRecords[i].Holliday,             { }
      DateToStr(lMyRecords[i].HolliDate), { }
      TimeToStr(lMyRecords[i].HolliHour)  { }
      ])                                  { }
      );
end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×