0

I was just wondering if a construct like the following c# function call is possible in Delphi:

C# code:

MyFunction(Param1, new MyClass() {property1 = "value1", property2 = true, property3 = 100}, Param3);

As for now in Delphi I should do the following to get that result:

Delphi code:

var
  aMyClass: TMyClass;
begin
  aMyClass:= TMyClass.Create;
  aMyClass.property1:= 'value1';
  aMyClass.property2:= True;
  aMyClass.property3:= 100;
  MyFunction(Param1, aMyClass, Param3);
end;

Much more verbose.

Help appreciate.

1
  • And be sure that you pass the aMyClass by reference Commented Jan 14, 2017 at 11:28

1 Answer 1

2

In Delphi the only way to achieve similar brevity is to define a constructor with parameters to receive the property values.

Sign up to request clarification or add additional context in comments.

1 Comment

@Marco Carboni For what it's worth, although it doesn't help with any Delphi work, this is only a limitation of the Delphi implementation of Pascal. The Oxygene implementation supports this directly using inline initialisers. You can simply add property assignments in any ctor call so if there is only a default, parameterless ctor then in this case something similar to: MyFunction(param1, new MyClass( property1 := 'value', property2 := true), param3). Oxygene also supports dynamic objects, which you may also come across in C# code.

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.