Delphi_developer
asked on
How to fill array with TArray.Create?
Hi
I'm trying to figure out how to fill array with TArray.Create. On simple types of array works:
this creats a simple array of integers with 1,2,3 integers.
What if I have record like this:
I tried with:
But it doesn't compile, it says:
Is there something I'm missing?
I use Delphi XE7.
I'm trying to figure out how to fill array with TArray.Create. On simple types of array works:
this creats a simple array of integers with 1,2,3 integers.
var Numbers:TArray<integer>;
...
Numbers:=TArray<integer>.Create(1,2,3);
What if I have record like this:
TExpert = record
ID:integer;
Name:string;
Active:boolean;
end;
var Experts:TArray<TExpert>;
I tried with:
Experts:=TArray<TExpert>.Create([1, 'Delphi expert',True]);
But it doesn't compile, it says:
[dcc32 Error] Unit1.pas(41): E2010 Incompatible types: 'Integer' and 'string'
Is there something I'm missing?
I use Delphi XE7.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well if your experts variable values is known at design time you could do this:
In order to be able to advise you better we need to understand the nature of your list. Is it predetermined, are you loading from a database, will it change, etc? I suspect you will be better served by using your own custom list derived from TList or TStringList with methods to add, remove, etc.
Var Experts: array[0..4] of TExpert = (
(ID: 1; Name: 'Delphi Expert'; Active: True)
, (ID: 2; Name: '.Net Beginner Expert'; Active: True)
, (ID: 3; Name: '.Net Advanced Expert'; Active: True)
, (ID: 100; Name: 'Java Expert'; Active: False)
, (ID: 101; Name: 'PHP Expert'; Active: False)
);
In order to be able to advise you better we need to understand the nature of your list. Is it predetermined, are you loading from a database, will it change, etc? I suspect you will be better served by using your own custom list derived from TList or TStringList with methods to add, remove, etc.
ASKER
Correct, this is for known list of values.
You could simply use the example above to create dinamically your experts by a function.
Take a look at this
Take a look at this
type
TExpert = record
ID: integer;
Name: string;
Active: boolean;
constructor Create(aID: integer; aName: String; aActive: boolean);
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function AddExpert(eId: integer; eName: String; eActive: boolean): TExpert;
public
{ Public declarations }
end;
var
Form1: TForm1;
ExpertArray: TArray<TExpert>;
implementation
{$R *.dfm}
constructor TExpert.Create(aID: integer; aName: String; aActive: boolean);
begin
ID := aID;
Name := aName;
Active := aActive;
end;
function TForm1.AddExpert(eId: integer; eName: String; eActive: boolean): TExpert;
begin
result := TExpert.Create(eId, eName, eActive);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ExpertArray := TArray<TExpert>.Create(AddExpert(1, 'Delphi Expert', True),
AddExpert(2, '.Net Beginner Expert', True),
AddExpert(3, '.Net Advanced Expert', True),
AddExpert(100, 'Java Expert', False),
AddExpert(101, 'PHP Expert', False)
);
end;
ASKER
Nice, so many options. I like to keep the lists like this in a code that very clearly shows the values, without needing to look too much into what it is, but very quickly to see the values, add or change.
So, even though Pierre's solution to just create variable with all the values is good as you don't need additional code to create data, but the list is not as clear, since the field names are in the same line.
So, now I like this example:
New in shorter than Create :)
This means even if I have 50 records, is still quite easy to see the values.
So, even though Pierre's solution to just create variable with all the values is good as you don't need additional code to create data, but the list is not as clear, since the field names are in the same line.
So, now I like this example:
ExpertsArray2 := TArray<TExpert > .Create(
TExpert.New(1, 'Delphi Expert', True),
TExpert.New(2, '.Net Beginner Expert', True),
TExpert.New(3, '.Net Advanced Expert', True),
TExpert.New(100, 'Java Expert', False),
TExpert.New(101, 'PHP Expert', False)
);
New in shorter than Create :)
This means even if I have 50 records, is still quite easy to see the values.
Well for easier reading some intending would have achieved the same thing.
See here, still easy to read:
I suppose from a programmer point of view it comes down to the intrinsic difference here. One is initialized at runtime where the other is created at design time so performance and memory usage issues are to be considered. However, in your case it does not sound like you have a big list to work with so I doubt it will have a major impact either way.
If it were me, I would have rather maintained the list in a file (e.g. csv, typed file, excel spreadsheet) and derive TExperts class from a TList (or TStringList) class with methods to load from file, add, remove, clear etc. In my opinion it is just easier to maintain a list like yours in a text file or spreadsheet, rather than within source code.
See here, still easy to read:
var
Experts: array[0..4] of TExpert = (
(ID: 1; Name: 'Delphi Expert'; Active: True)
, (ID: 2; Name: '.Net Beginner Expert'; Active: True)
, (ID: 3; Name: '.Net Advanced Expert'; Active: True)
, (ID: 100; Name: 'Java Expert'; Active: False)
, (ID: 101; Name: 'PHP Expert'; Active: False)
);
I suppose from a programmer point of view it comes down to the intrinsic difference here. One is initialized at runtime where the other is created at design time so performance and memory usage issues are to be considered. However, in your case it does not sound like you have a big list to work with so I doubt it will have a major impact either way.
If it were me, I would have rather maintained the list in a file (e.g. csv, typed file, excel spreadsheet) and derive TExperts class from a TList (or TStringList) class with methods to load from file, add, remove, clear etc. In my opinion it is just easier to maintain a list like yours in a text file or spreadsheet, rather than within source code.
ASKER
I agree with indenting, but hard to achieve with GExperts' Code Formatter that i use regularly.
External file sounds useful, but I like to keep this in the code.
External file sounds useful, but I like to keep this in the code.
ASKER
Thank you guys!
The GExperts code formatter supports {(*} and {*)} to mark code you don't want it to touch.
ASKER
It would be nice if it has a feature to format like that, to align by columns. Is that possible or too complex?
It's possible, just nobody has bothered to implement it yet. You are welcome to get the source code and do it.
ASKER
Maybe I'll work on it, when my current project ends.
ASKER
Both examples show how it's done. And it's pretty good, although a lot more code than I was hoping for.
I should have add to my original question the example of what I'm doing, and here is where i thought I could this with - this is how I add info about Experts:
Open in new window
So, I was hoping to skip the AddExpert procedure. Well, I can't really avoid it, I can only replace it with suggestions above, to create variables of the record type or use Constructor.