Reputation: 1342
I have an excel sheet that needs converting into a C# class. The idea here is that for each row in excel sheet, I will create an instance of the class passing Product Id and Period id as parameters in the constructor. So for each Product in that particular period, an instance will be created.
This class has several properties. One property requirement is that in its formula, another property's Total is to be called.
So for example, my sheet is like this:
I need to get the % of Production in each instance.
If my class is called clsProduction, how can I populate its property 'ProductionPerc' once an instance of this class is created?
Any idea will be highly appreciated.
My code is here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace IMS
{
public class clsProduction
{
public clsProduction() { }
public clsProduction(clsCostOfCrudeMVG obj, int _PeriodId, int _ProductId)
{
_clsCostOfCrudeMVG = obj;
PeriodId = _PeriodId;
ProductId = _ProductId;
GetQuantity();
}
public int ProductionPerc
{
get { return _ProductionPerc; }
set { _ProductionPerc= value; }
}
}
}
Upvotes: 0
Views: 842
Reputation: 1649
you must use 2 classes, Since a production exists out of details you should do it that way.
public class productionDetail
{
public string ProductName {get;set;}
public int ProductionQuantity {get;set;}
public double ProductionPercentage {get;set;}
public productionDetail(string productName, int productQuantity)
{
ProductName = productName;
ProductionQuantity = productQuantity;
ProductionPercentage = 0; // This will change later on
}
}
public class Production
{
public List<productionDetail> Details {get;set;}
public int TotalProduction {get;set;}
public production (List<productionDetail> myDetails)
{
Details = myDetails;
RecalculatePercentage();
}
//Here the total will be calculated
public void MakeTotal()
{
var totalProduction = 0;
foreach(productionDetail d in Details )
{
totalProduction += d.ProductionQuantity;
}
TotalProduction = totalProduction;
}
public void RecalculatePercentage()
{
MakeTotal();
//Here you will update the detail records for the percentage.
foreach(productionDetail d in Details )
{
d.ProductionPercentage = Convert.ToDouble(d.ProductionQuantity) / Convert.ToDouble(TotalProduction) * 100;
}
}
public void AddDetail(productionDetail detail)
{
Details.Add(detail);
RecalculatePercentage();
}
}
Upvotes: 1
Reputation: 9122
Each instance of your class only knows about itself. It does not know about the 'population' and therefore cannot make any calculation about what percentage of the overall 'population' the value represent.
You need a class to actually do those calculations (much like the Excel 'Application' makes calculations based on the contents of cells).
Try this:
Class ProductionManager
{
List<ProductionItem> _ProductionItems
AddProductionItem(ProductionItem)
{
// Add the production items to _ProductionItems List
// Now
// 1) enumerate the current collection of _ProductionItems
// 2) keep running totals
// 3) now re-enumerate the current collection of _ProductionItems
// updating each item with its respective percentage of the totals
// you calculated in step 2.
// and populate each ProductionItem with the respective percentages
}
}
Upvotes: 1