tags:

views:

48

answers:

2

i have some questions when watching this tutorial.

i wonder when i overwrite methods, how do i know if i need to call the base method?

public CustomerCollection(IEnumerable<Customer> customers, OMSEntities context) : base(customers)

also why do i need to do

protected override void InsertItem(int index, Customer cust)
{
    this.context.AddToCustomers(cust);
    base.InsertItem(index, cust);
}

protected override void RemoveItem(int index)
{
    this.context.DeleteObject(this[index]);
    base.RemoveItem(index);
}

what does the 2 lines in each method do? and why the need for such similar method. if i overwrite methods for delete and add why not update too?

+4  A: 

You call the base method when you're method is just additional functionality decorating what the base method does, you don't call the base method when you are replacing it's functionality wholesale.

And if you don't know what the base method does, you wouldn't be overriding it, so in knowing what it does and why you're overriding it, you should be able to make this decision in accordance.

Jimmy Hoffa
then about the 2nd question? "what does the 2 lines in each method do? and why the need for such similar method. if i overwrite methods for delete and add why not update too?"
jiewmeng
@jiewmeng: Probably because the this.context already has a reference to the object so changes to it will appear to the this.context object, so it doesn't need to be added or removed, and so the base class updating the item is enough. I can't say as though I know what "this.context" refers to precisely.
Jimmy Hoffa
context is the "Entity Container Name" of my data model, i dunno whats that too
jiewmeng
A: 

For the first part of your question if you do not add a :base(parameter) to the end of a constructor it will actually put a :base() there for you behind the scenes. if there is no base() you will get a compile exception. you need a chain of constructors all the way down to object(). adding that :base(parameter) is just a way of choosing a different constructor other than the default one.

For the second part. It is a good rule of thumb that if you are overriding a method to provide some kind of additional functionality you should call the base method instead too (so you get its functionality), if you are trying to replace the functionality instead of add to it you do not need to call the base(as you are replacing it :) ).

Scott Chamberlain
then about the 2nd question? "what does the 2 lines in each method do? and why the need for such similar method. if i overwrite methods for delete and add why not update too?"
jiewmeng
The first line is is finding the object context in the current class (this could be in your custom class or in the parent if it is a protected or public item) and either calling AddToCustomers and passing cust to it or calling DeleteObject and passing this[index]. Then after that it will call the base version of the function passing the same parameters. So this is basically like copying the first line and editing the base function and pasting it at the top
Scott Chamberlain