?
Solved

c# generic syntax don't understand

Posted on 2015-02-03
6
Medium Priority
?
174 Views
Last Modified: 2015-02-03
Can anyone explain this to me...I'm too dumb! This is from the telerik mvc grid extensions, you make columns with
Grid.Columns(columns=>{
columns.Bound("xx");etc
}
I put a break in the Columns func, and its sig is:
Columns(Action<GridColumnFactory> configurator)
Now, I understand, Action<T> is a delegate void method, but what I don't understand is how columns=>{
columns.Bound("xx"); }is implicitly cast to GridColumnFactory?
(GridColumnFactory- a class which has params in its constructor )
0
Comment
Question by:Silas2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40586143
It isn't being implicitly cast - it is explicitly declared by the Columns() method.

If you look at the documentation for the Columns() method, you'll see that it takes a parameter of type Action<GridColumnFactory<T>> (reference: http://www.telerik.com/help/aspnet-mvc/m_telerik_web_mvc_ui_fluent_gridbuilder_1_columns.html) where T is the model that the grid is being bound to.
0
 

Author Comment

by:Silas2
ID: 40586297
Ok, its in the function sig in the docs, but how does columns=>{ columns.bound("xx"; columns.bound("yy");} become acceptable as GridColumnFactory? it doesn't look anything like the factory.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40586454
columns (the left-hand operator version) is created at runtime implicitly based on the context.  The runtime can figure out the type from the context.

So, in you case, columns will actually be an object of type GridColumnFactory<TModel>. Bound() is a method on the factory class.

Hope that makes some sense.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Silas2
ID: 40586630
Most kind, yes Its certainly helping.

So when the param comes into the call it (the runtime) hasn't instanced the factory even though it's (or something is) in the call.

In the call , (which is on the GridBuilder obj), this is the first line:
configurator(new GridColumnFactory(Component, Context));

(configurator is the method param)

Aah, the penny (half) dropped as I was typing, so the configurator is an Action<GridColumnFactory<T>>, does that mean the default call is to make a new one? Factory I mean, so does that mean if x = Action<Type>, then x(new Type())?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 40586671
>> so the configurator is an Action<GridColumnFactory<T>>, does that mean the default call is to make a new one?

Yes, that's the jist of it.  Because it is a Lambda expression, the underlying mechanics will instantiate a new object of type Action<GridColumnFactory<T>>, and that becomes the context for the expression.  So, anything to the right of => is using an instance of the GridColumnFactory<T> object to make calls on.
0
 

Author Comment

by:Silas2
ID: 40586769
So, from the docs:
Action<string> messageTarget;
messageTarget = s => Console.WriteLine(s);

So with the grid:
Action<GridColumnFactory<T>> x;
x=>{ x.Bound(1); x.Bound(2); }

Ah, I think I get it, for each x.Bound call, its using the factory's bound call. So the call into 'Columns', is receiving the right param, not because its been explicitly sent, but because the receiving procedure but because of its signature.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question