-2

I am trying to better understand chaining of constructors in C# and I have run into the following issue.

class Item
{
    private string _name;
    private string _category;
    private int _sku;
    private double _price;

    // default values
    public Item()
    {
        _name = "";
        _category = "Sale Item";
        _sku = 123;
        _price = 1.99;
    }

    public Item(string name, double price) : this()
    {
        this._name = name;
        this._price = price;
    }

    public Item(string name, string category, int sku, double price)
    {
        this._name = name;
        this._category = category;
        this._sku = sku;
        this._price = price;
    }

    public string Name
    {
        get { return this._name; }
    }

    public string Category
    {
        get { return this._category; }
    }

    public int SKU
    {
        get { return this._sku; }

    public double Price
    {
        get { return this._price; }
    }
}

My idea was to use the parameterless constructor to set default values and use the parametrized constructors to only change those values which need to be updated.

Unfortunately this does not work. The code does not compile. The error message is 1729: there is no constructor that takes 2 arguments. I realize that this is not how constructors are normally chained but I do not understand why this fails to compile as the parameterless constructor Item() is called first before the second constructor Item(string name, double price) is called.

Any insight and sugegstions would be greatly appreciated.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
PBrenek
  • 561
  • 1
  • 8
  • 24
  • 3
    your code gives compiler errors because of SKU and Price properties. other than that it compiles fine.show your actual code. – Selman Genç Feb 26 '15 at 21:57
  • 2
    Please also include the code where the compile error actually occurs. – ryanyuyu Feb 26 '15 at 21:58
  • 2
    The code compiles and runs fine for me, other than that you're missing a bracket in your class definition and that two properties aren't of the right type. Please provide an example that demonstrates the described problem. – Servy Feb 26 '15 at 21:58
  • @Selman22: I am not sure how the properties SKU and Price would be a problem when the compiler complains about the number of arguments in the constructor, – PBrenek Feb 26 '15 at 22:01
  • @PBrenek Those properties were of type `string` now that you've fixed that..... – juharr Feb 26 '15 at 22:04
  • Yes. The code is correct, I made typos here. My apologies. – PBrenek Feb 26 '15 at 22:05
  • It's good you've shown the class, but you aren't showing the code that's generating the error (where you're trying to instantiate the class). The only 2-parameter constructor is one which takes a `string` and a `double`. Without seeing your code, my best guess is that you are passing some other data type(s) to it instead. – Rufus L Feb 26 '15 at 22:05
  • @Rufus L: Interesting. I had not thought of that. I instantiate the class as follows: Item beachBall = new Item("Beach Bal", 3.49); – PBrenek Feb 26 '15 at 22:07
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 26 '15 at 22:12
  • @PBrenek It should compile with `Item bleachBall = new Item("Beacch Bal", 3.49);`. There is nothing wrong with the code except one parenthesis missing. – Bikal Bhattarai Feb 26 '15 at 22:15
  • The question is downvoted. Really? – PBrenek Feb 26 '15 at 22:38

1 Answers1

1

Nothing wrong with the chaining constructors per se, the error you get is related to other code instantiating it with 2 specific paramaters which their is no specific constructor provided.

You need to add another 2 parameter constructor which matches that signature to fix that error.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122