Marcol1no
Marcol1no

Reputation: 47

Delphi Storing data in Array and retrieve it

i need to store a two strings in an Array like this, and retrieve values in another function to use it.

Date Start Date End
VALUE_START VALUE_END
VALUE_START2 VALUE_END2

I think the correct ways is to use this variable StoringData : TArray<String>, but how can i valorized it with VALUE_START and VALUE_END ?

Upvotes: 0

Views: 987

Answers (2)

fpiette
fpiette

Reputation: 12322

First you have to define a data type that can hold two string:

type
    TPeriod = record
        DateStart : String;     // Isn't TDateTime better than String here ?
        DateEnd   : String;
    end;

Then you can declare a dynamic array if that data type

var
    StoringData : TArray<TPeriod>;

At some point, you must allocate space for the array (This is a dynamic array so no space allocated when it is declared)

SetLength(StoringData, TheSizeYouNeed);

The you can access the array elements like this:

StoringData[0].DateStart := '04/05/2021';
StoringData[0].DateEnd   := '06/05/2021';
StoringData[1].DateStart := '04/06/2021';
StoringData[1].DateEnd   := '06/06/2021';

You don't need to free the allocated space, it will be done automatically, but you can if you need to reclaim memory. Call SetLength with a zero size.

You can resize the array. Existing data will be kept (If you size down, some will of course be lost):

SetLength(StoringData, 3);   // Add a third more row
// Assign a value
StoringData[2].DateStart := '04/07/2021';
StoringData[2].DateEnd   := '06/07/2021';

Upvotes: 2

HeartWare
HeartWare

Reputation: 8261

Make a 2-dimensional array with the first dimension being dynamic, and the second one static:

TYPE
  TPeriod = ARRAY[1..2] OF STRING;
  TPeriods = TArray<TPeriod>;

VAR
  Periods : TPeriods;

BEGIN
  SetLength(Periods,2);
  Periods[0,1]:='Start Date 1';
  Periods[0,2]:='End Date 1';
  Periods[1,1]:='Start Date 2';
  Periods[1,2]:='End Date 2';
END.

Upvotes: 0

Related Questions