0

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 ?

2
  • type TPeriod = record StartDate, EndDate: TDate; end and var Data: array[0..1] of TPeriod if you want a static array and var Data: TArray<TPeriod> if you want a dynamic array. Remember that dynamic arrays are reference types. Or, use a TList<TPeriod> if you want a simpler API to work against and can live with having to create and free the list heap object. Commented May 28, 2021 at 9:01
  • 1
    And I might also advice you to buy a good book about Delphi programming and read it carefully so you learn the language properly! Commented May 28, 2021 at 9:15

2 Answers 2

2

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';
Sign up to request clarification or add additional context in comments.

10 Comments

That's what I was looking for, my code right now is this StoringData : TArray<String>; SetLength(StoringData,2); for x := 0 to High(StoringData) do for c := 0 to High(StoringData[x]) do begin StoringData[x] := DataCovertedS; StoringData[c] := TotTime; ` end;`
No need for a record type. Use a 2-dimensional array instead, if you like
@DelphiCoder Yes 2D array, but i'm not able to store [c] on the second column do you know why ?
A record is much better than a 2D array: a record can have methods ! For example, you can add a method to initialize the record at once, or to get/set the values from/to TDateTime and much more. Also with a record, you can easily add more members without breaking existent code. Records in Delphi are first class citizen, much like classes. 2D array are old school.
@Marcol1no I edited my answer to add a resize example (adding a 3rd row).
|
0

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.

1 Comment

Much less readable than a record, much more prone to errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.