2

as you know initializing object in c# is really handy and fast

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

and

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

is it possible to initializing objects in Delphi like this?

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
vesal
  • 243
  • 4
  • 11

4 Answers4

5

As pointed out by others there is no object initializer syntax like there is in C#.

There are a few alternatives that come close.

  1. ja-mesa already pointed out the with construct, though it's best to avoid this construct. You can see my blog for a (mostly) unbiased review of the pros and cons of using with as well as similar constructs in other languages and some alternatives.
  2. Anonymous methods can be used for this though they're a little verbose and kind of ugly:

    TMyObject.Create(procedure(var  FirstName, LastName: string)
                     begin
                       FirstName := 'Craig';
                       LastName := 'Playstead';
                     end);
    
  3. A fluent interface can come fairly close to approximating this:

    TMyObject.Create
             .FirstName('Craig')
             .LastName('Playstead');
    

    The downside being that writing a fluent interface is time consuming and only pays off if you plan on using this class a lot or are writing a public api.

  4. Constant records also come very close.

    const
      MyRecord: TMyRecord =
      (
        FirstName : 'Craig';
        LastName : 'Playstead';
      );
    

    The obvious drawback being that it is a constant

  5. Another solution would be an overloaded constructor:

    TMyObject.Create('Craig', 'Playstead');
    

Of course you could accomplish much the same thing by simply creating a temporary variable with a single character name.

var
  o: TMyObject;

begin
  o := TMyObject.Create;
  o.FirstName := 'Craig';
  o.LastName := 'Playstead';
  Result := o;
end;
Community
  • 1
  • 1
Kenneth Cochran
  • 11,954
  • 3
  • 52
  • 117
4

No, there is no direct equivalent in Delphi, there is something like this:

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;
ja_mesa
  • 1,971
  • 1
  • 11
  • 7
  • A great example of 'with', although the keyword is [slightly dangerous.](http://stackoverflow.com/questions/514482/is-delphi-with-keyword-a-bad-practice) – David Jul 21 '13 at 18:22
1

You can override and reintroduce constructor Create. I'd recommend reading this fine answer here on StackOverflow.

Community
  • 1
  • 1
  • That isn't what the question asks. This doesn't need any special help from the class being initialised in C#. –  Jul 21 '13 at 10:52
  • i know what constructor do my friend, actually my question is how to create object without declaring local variable and directly pass it to parameter argument like what i did in c# – vesal Jul 21 '13 at 11:16
  • 3
    I think what Mark means is that you can pass the parameters into the constructor, so have it all on a single line. @Mark, perhaps you could update your answer with more info? – David Jul 21 '13 at 18:21
1

There is no equivalent syntax in Delphi. You must either reintroduce the constructor to add new parameters to it, or else construct the object first and then assign its members/properties as needed separately.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770