0

I have a class that is dealing with objects i have created for example;

StockItem2 = new CarEngine("Mazda B6T", 1252, 8025, 800, "Z4537298D");
//StockItem2 = new CarEngine("description", cost, invoice #, weight, engine #)

I also have a static int setting the last invoice number to 10,000.

 internal static int LastStockNumber = 10000; 

If no invoice number is entered, i want it to auto assign one and increment by 1 each time, so 10,001 / 10,002 etc..

CarEngine has 2 constructors, this is the one that is meant to assign the invoice number if one is not entered. It takes the description, cost, weight, engine number and should autoassign from 10,001 onward but it seems to be incrementing by 2-3 at a time, any ideas why?

public CarEngine(string Description, int CostPrice, int Weight, string EngineNumber)
        : base(Description, CostPrice, Weight)
    {
        LastStockNumber++;
        StockNumber = LastStockNumber;
        this.CostPrice = CostPrice;
        this.Description = Description;
        this.Weight = Weight;
        this.EngineNumber = EngineNumber;
    }

// this is in the stockitem class //
public StockItem(string Description, int CostPrice)
    {
        LastStockNumber++;
        StockNumber = LastStockNumber;            
        this.Description = Description;
        this.CostPrice = CostPrice;
    }
//this is in the heavystockitem class//
public HeavyStockItem(string Description, int CostPrice, int Weight) :     base(Description, CostPrice)
    {

        StockNumber = LastStockNumber;
        LastStockNumber++;
        this.CostPrice = CostPrice;
        this.Description = Description;
        this.Weight = Weight;
    }
2
  • I will add the other constructors of the other 2 classes, they pretty much do the same thing.. but my guess is that its auto assigning for example 10,001 then it adds 1 and adds 1 again and assigns 10,003 etc Commented Oct 2, 2013 at 5:15
  • 1
    You are incrementing the LAstStockNumber in consturctor, so even if user enter the Invoice Number, it still get increment, and you are incrementing in every class, means whatever object you assign, Stock number will increase. You should better create a Static Function in one of core/common class to get the "Invoice ID" when it is not already given, rather than having it in all class. Commented Oct 2, 2013 at 5:25

2 Answers 2

2

You may be incrementing LastStockNumber in base class and in derived class constructor.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, i removed the increment from the base class and it is properly adding now. :)
Isn't there more logic in removing increment from derived class?
1

I'm guessing from the signatures given that your CarEngine inherits from HeavyStockItem which inherits from StockItem.

Given this, when creating a new CarEngine object, it will call the base constructor, which will call its own base constructor.

Since each constructor has the following code: LastStockNumber++; then for a CarEngine the number will be incremented 3 times, for a HeavyStockItem it would be incremented twice, and for a StockItem it would only be incremented once.

Given that you're calling the base constructors you should only initialize things unique to your class. Try changing your code to the following:

public CarEngine(string description, int costPrice, int weight, string engineNumber)
    : base(description, costPrice, weight)
{
    EngineNumber = engineNumber;
}

//this is in the heavystockitem class//
public HeavyStockItem(string description, int costPrice, int weight)
    : base(description, costPrice)
{

    Weight = weight;
}

// this is in the stockitem class //
public StockItem(string description, int costPrice)
{
    LastStockNumber++;
    StockNumber = LastStockNumber;
    Description = description;
    CostPrice = costPrice;
}

For bonus points, note that I've changed your constructor parameters from PascalCase to camelCase, as per generally accepted C# standards. This helps you distinguish between Properties (first letter uppercase) and parameters (first letter lowercase).

2 Comments

Oh, i always forget to do that!.. Do i do that for constructors or methods also? I can't remember
With methods, a pattern is to sometimes call base.MethodName(param1, param2); on the first line, if you are doing that then you don't want to double-up on your work, just do the extra work needed by your current class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.