Author Topic: Calling Functions in Object Initializers?  (Read 2319 times)

0 Members and 1 Guest are viewing this topic.

DavidS

  • Mosquito
  • Posts: 15
Calling Functions in Object Initializers?
« on: August 17, 2016, 10:13:23 AM »
I been studying some of Scott McFarlane's code and was wondering if someone could help me find the C# documentation that discusses how the C# compiler knows that Scott is calling the "Paletteset.Add(string name, Control control)" function in the following code.  All of the example code that I have found online do not acknowledge that calling a function in an object initializer is possible.

Code - C#: [Select]
  1.                 public void Initialize()
  2.                 {
  3.                         if (_paletteSet == null)
  4.                         {
  5.                                 var ctrl = new UserControl1();
  6.  
  7.                                 _paletteSet = new PaletteSet("WPF Demo")
  8.                                                 {
  9.                                                         {
  10.                                                                 "Circle Radius",
  11.                                                                 new ElementHost {AutoSize = true, Dock = DockStyle.Fill, Child = ctrl}
  12.                                                         }
  13.                                                 };
  14.  
  15.                                 ctrl.HostApplication = new HostApplication();
  16.                         }
  17.                         _paletteSet.Visible = true;
  18.                 }

ILSpy shows that the previous code is converted to this:

Code - C#: [Select]
  1. // DV2177.WPF.AcadHost.MainApp
  2. public void Initialize()
  3. {
  4.         if (MainApp._paletteSet == null)
  5.         {
  6.                 UserControl1 ctrl = new UserControl1();
  7.                 PaletteSet paletteSet = new PaletteSet("WPF Demo");
  8.                 paletteSet.Add("Circle Radius", new ElementHost
  9.                 {
  10.                         AutoSize = true,
  11.                         Dock = DockStyle.Fill,
  12.                         Child = ctrl
  13.                 });
  14.                 MainApp._paletteSet = paletteSet;
  15.                 ctrl.HostApplication = new HostApplication();
  16.         }
  17.         MainApp._paletteSet.set_Visible(true);
  18. }
  19.  

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Calling Functions in Object Initializers?
« Reply #1 on: August 17, 2016, 09:49:36 PM »
All of the example code that I have found online do not acknowledge that calling a function in an object initializer is possible.

Could you please show an example of such documentation?
The Initializer is where many many functions/methods are called. For example, take a look at a winform designer code file, this is where all the controls are added and delegates assigned all via methods.
In your partial class Initialize() code is where you may call many functions to set up your member variables and any other set up calculations.

Or, maybe I don't understand your question correctly??
cheers.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

DavidS

  • Mosquito
  • Posts: 15
Re: Calling Functions in Object Initializers?
« Reply #2 on: August 19, 2016, 02:16:46 PM »

Hi MickD,

I failed to fully describe what I was talking about plus I had a function named "Initialize", so I'm sorry for the confusion. 

Here is a link to C# feature that I am talking about:
How to: Initialize Objects by Using an Object Initializer (C# Programming Guide)https://msdn.microsoft.com/en-us/library/bb397680.aspx

In some of their examples they are using the compiler created default constructor to create a new Student with and "Object Initializer" to establish the properties of that student.
Code - C#: [Select]
  1.  new StudentName {FirstName="Craig", LastName="Playstead", ID=116}

Each property is named and there is no mention in this web page of calling a function named or unnamed inside of the "Object Initializer". 

The part of the previous post that I was trying to call attention to was this part which is in the "Object Initializer" portion of the PaletteSet(string name) constructor.
Code - C#: [Select]
  1. {
  2.         "Circle Radius",
  3.         new ElementHost {AutoSize = true, Dock = DockStyle.Fill, Child = ctrl}
  4. }

The intended function is not named but it appears from ILSPY that the compiler is using the variable types to choose this function:
Code - C#: [Select]
  1. public Palette Add(string name, Control control);


MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Calling Functions in Object Initializers?
« Reply #3 on: August 19, 2016, 05:52:11 PM »

The part of the previous post that I was trying to call attention to was this part which is in the "Object Initializer" portion of the PaletteSet(string name) constructor.
Code - C#: [Select]
  1. {
  2.         "Circle Radius",
  3.         new ElementHost {AutoSize = true, Dock = DockStyle.Fill, Child = ctrl}
  4. }

Hi DavidS, thanks for the clarification and you are correct, the object initializer is using the variable type. The extra double braces "{}" that wrap the name and element host are "collection initializers", as a PaletteSet is a collection it knows what to do with them and you can also have multiple initializers in your constructor.

cheers,
Mick
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

n.yuan

  • Bull Frog
  • Posts: 348
Re: Calling Functions in Object Initializers?
« Reply #4 on: August 19, 2016, 06:34:38 PM »

Hi MickD,

I failed to fully describe what I was talking about plus I had a function named "Initialize", so I'm sorry for the confusion. 

Here is a link to C# feature that I am talking about:
How to: Initialize Objects by Using an Object Initializer (C# Programming Guide)https://msdn.microsoft.com/en-us/library/bb397680.aspx

In some of their examples they are using the compiler created default constructor to create a new Student with and "Object Initializer" to establish the properties of that student.
Code - C#: [Select]
  1.  new StudentName {FirstName="Craig", LastName="Playstead", ID=116}

Each property is named and there is no mention in this web page of calling a function named or unnamed inside of the "Object Initializer". 

The part of the previous post that I was trying to call attention to was this part which is in the "Object Initializer" portion of the PaletteSet(string name) constructor.
Code - C#: [Select]
  1. {
  2.         "Circle Radius",
  3.         new ElementHost {AutoSize = true, Dock = DockStyle.Fill, Child = ctrl}
  4. }

The intended function is not named but it appears from ILSPY that the compiler is using the variable types to choose this function:
Code - C#: [Select]
  1. public Palette Add(string name, Control control);

In this code:

Code - C#: [Select]
  1.  new StudentName {FirstName="Craig", LastName="Playstead", ID=116}

Each property is accessed/identified by its name,

While in this code:

Code - C#: [Select]
  1. {
  2.         "Circle Radius",
  3.         new ElementHost {AutoSize = true, Dock = DockStyle.Fill, Child = ctrl}
  4. }

method is accessed/identified by its signature, as long as it is unique. If a class has more than one methods having the same signature, then they have to be accessed/identified by name. Since PaletteSet only has one method that takes 2 arguments - first is a String, second a Winform Control, thus, its name can be omitted.  This is a C# language feature introduced in C# 3.0 when .NET 3.5 was released, I believe. I am not sure how do to it in VB.NET (probably something like "With...End With"?).

Either Class's Property, or Method, is really another class (meaning, everything is a object/class), thus the code:

var class1Instnace=new Class1 { Property1=expression1, Property2=expression2, Method1(arg1, arg2), Method2(arg1,arg2), {arg3, arg4, arg3},...};

In this case, Class has defined 3 methods: 2 methods accept the same types of 2 argument, while the 3rd method accept 3 argument. Therefore, in the "object initializer", if the code need to call all 3 methods, the first 2 have to be referred by name, and the third does not need to be called by name because of its unique signature. Of course, in the "object initializer", the required process logic decide which property to be initialized, which method need to be called.