ご存じのとおり、C# でのオブジェクトの初期化は非常に便利で高速です
StudentName student2 = new StudentName
{
FirstName = "Craig",
LastName = "Playstead",
};
と
List<MyObject>.Add(new MyObject{a=1,b=2})
このように Delphi でオブジェクトを初期化することは可能ですか?
ご存じのとおり、C# でのオブジェクトの初期化は非常に便利で高速です
StudentName student2 = new StudentName
{
FirstName = "Craig",
LastName = "Playstead",
};
と
List<MyObject>.Add(new MyObject{a=1,b=2})
このように Delphi でオブジェクトを初期化することは可能ですか?
いいえ、Delphi には直接同等のものはありません。次のようなものがあります。
Student2:=StudentName.Create();
with Student2 do
begin
FirstName:= 'Craig';
LastName:= 'Playstead';
end;
MyObject:=TMyObject.Create();
With MyObject do
begin
a:=1;
b:=2;
end;
List.Add(MyObject);
// Although, there is something in Delphi that I haven't found in C++, Java or C#
With TMyObject.Create do
try
// You can access TMyObject properties and method in here
finally
Free;
end;
// For example:
With TMyModalForm.Create(Self) do
try
if ShowModal() = mrOK then
begin
// etc
end;
finally
Free;
end;