XSharpPublic icon indicating copy to clipboard operation
XSharpPublic copied to clipboard

Cannot use event assignment in object initializer

Open cpyrgas opened this issue 2 years ago • 1 comments

https://www.xsharp.eu/forum/public-product/3619-event-registration-in-class-initializer#27330

This compiles without errors:

oMenuItem := MenuItem{}{ Name := "Exit", Checked := FALSE}

This one:

oMenuItem := MenuItem{}{ Name := "Exit", Checked := FALSE, Click += test}

FUNCTION Test(o AS OBJECT, e AS EventArgs) AS VOID

reports:

error XS1922: Cannot initialize type 'System.Windows.Forms.MenuItem' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
error XS0747: Invalid initializer member declarator
error XS0103: The name 'Name' does not exist in the current context
error XS0747: Invalid initializer member declarator
error XS0103: The name 'Checked' does not exist in the current context
error XS0747: Invalid initializer member declarator
error XS0103: The name 'Click' does not exist in the current context

cpyrgas avatar Aug 17 '23 05:08 cpyrgas

Hmm, looks like it's not supported in c# either, but it's on the todo list:

https://github.com/dotnet/csharplang/discussions/307 https://github.com/dotnet/csharplang/issues/5176

maybe we keep this incident open, for when/if the feature gets implemented in Roslyn

cpyrgas avatar Aug 17 '23 05:08 cpyrgas