views:

62

answers:

3

I'm trying to understand the observer pattern using C#, first I have an abstract class working as Subject called Stock, then I'm creating a concreteSubject class so I'm going to call it IBM, as the concreteSubject Class is going to inherit from Stock, so I do something like this:

class IBM : Stock
{
    // Constructor
    public IBM(string symbol, double price)
      : base(symbol, price)
    {
    }
}

what I don't understand is the " : base(symbol, price) " why should I use it? what that means? it looks like its inherit the symbol and price variables, but why if they are declared as parameters on the public IBM function

I get this code from an example that I found in:

http://www.dofactory.com/Patterns/PatternObserver.aspx#_self1

+2  A: 

It calls base class (Stock) constructor. If you look in Stock class code it looks like this

public class Stock {
    private string _symbol;
    private double _price;

    public Stock(string symbol, double price)  // this constructor is called
    {
         this._symbol = symbol;
         this._price = price;
    }
} 

Note that it is only constructor in Stock class so you must call it explicit in all derived classes by base(symbol, price).

jethro
Ok I see, I need to call the Stock's class contructor from the IBM constructor so I'm passing the parameters to it using the statement ": base(symbol, price)"Thank you
KnightL3on
A: 

This construct means that the IBM constructor calls the Stock constructor using the same parameters.

There would normally be some extra code in the IBM constructor.

There's some examples on the MSDN Using Constructors (C# Programming Guide)

ChrisF
Good, now I understand, as my IBM class is inherit from Stock and Stock has a constructor with that two parameters I'm passing the values to the Stock constructor from the IBM constructor that's why I use " : base(symbol, price) " I think I have this clear Thank you
KnightL3on
A: 

Hello,

Just check following resource for better understanding of that construction: Constructors in C#

Jarek Waliszko
Thank you for helping me, now I have clear this part
KnightL3on