2

ご存じのとおり、C# でのオブジェクトの初期化は非常に便利で高速です

    StudentName student2 = new StudentName
    {
        FirstName = "Craig",
        LastName = "Playstead",
    };

List<MyObject>.Add(new MyObject{a=1,b=2})

このように Delphi でオブジェクトを初期化することは可能ですか?

4

4 に答える 4

4

いいえ、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;
于 2013-07-21T17:04:29.520 に答える