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
VulpesPosted Feb 5, 2013, 2:38 PM
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);
Guest UserPosted Feb 6, 2013, 6:32 AM
Obviously, as the RHS and LHS of the assignment must be compatible i.e of same types.
VulpesPosted Feb 6, 2013, 5:53 AM
The ShoppingCartItems property would have a return type of Table
Guest UserPosted Feb 6, 2013, 4:49 AM
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...
Guest UserPosted Feb 5, 2013, 2:28 PM
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...
VulpesPosted Feb 5, 2013, 2:11 PM
SomeClass sc = property;
sc.Method();