Hello
Ive seen it in code a few times whereby a property member followed by a method is used...
The syntax is as below:-
property.Method();
or more specifically
db.ShoppingCartItems.InsertOnSubmit();
I just wondered how and why this chaining works\exists?
Regards
Steven
Loading

VulpesRank #55 · 29.2K reputationPosted Feb 5, 2013, 9:38 AM EST
It's a question of what type of object the db.ShoppingCartItems property is returning, not the type of object on which the property is called.
If this is LINQ to SQL code, then I'd imagine that it's returning a reference to a Table
So, splitting it up into two statements would give you:
Table
table.InsertOnSubmit(someItem);
VulpesRank #55 · 29.2K reputationPosted Feb 5, 2013, 9:11 AM EST
SomeClass sc = property;
sc.Method();
Guest UserRank #4,868 · 321 reputationPosted Feb 5, 2013, 9:28 AM EST
So in the example
db.ShoppingCartItems.InsertOnSubmit();
db has a ShoppingCartItems property as well as InsertOnSubmit method therefore the above is a shorthand notation or inline statement for:-
DB db = new DB();
db.ShoppingCartItems;
and
DB db = new DB();
db.InsertOnSubmit();
Combined...
Guest UserRank #4,868 · 321 reputationPosted Feb 5, 2013, 11:49 PM EST
So given
Table
table.InsertOnSubmit(someItem);
The ShoppingCartItems property would have a return type of ShoppingCartItem.
And the InsertOnSubmit method is simply being invoked from the table object as per normal syntax...
VulpesRank #55 · 29.2K reputationPosted Feb 6, 2013, 12:53 AM EST
The ShoppingCartItems property would have a return type of Table
Guest UserRank #4,868 · 321 reputationPosted Feb 6, 2013, 1:32 AM EST
Obviously, as the RHS and LHS of the assignment must be compatible i.e of same types.