Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OOP-获取同一类的另一个属性中一个属性的和_C#_Oop - Fatal编程技术网

C# OOP-获取同一类的另一个属性中一个属性的和

C# OOP-获取同一类的另一个属性中一个属性的和,c#,oop,C#,Oop,我有一张excel表格需要转换成C类。这里的想法是,对于excel工作表中的每一行,我将创建一个类的实例,在构造函数中传递产品Id和周期Id作为参数。因此,对于该特定时期内的每个产品,都将创建一个实例。 这个类有几个属性。一个属性要求是在其公式中调用另一个属性的总和 例如,我的工作表如下所示: 我需要得到每个实例的生产百分比 如果我的类名为clsProduction,那么在创建该类的实例后,如何填充其属性“ProductionPerc” 任何想法都将受到高度赞赏 我的密码在这里 using S

我有一张excel表格需要转换成C类。这里的想法是,对于excel工作表中的每一行,我将创建一个类的实例,在构造函数中传递产品Id和周期Id作为参数。因此,对于该特定时期内的每个产品,都将创建一个实例。
这个类有几个属性。一个属性要求是在其公式中调用另一个属性的总和

例如,我的工作表如下所示:

我需要得到每个实例的生产百分比

如果我的类名为clsProduction,那么在创建该类的实例后,如何填充其属性“ProductionPerc”

任何想法都将受到高度赞赏

我的密码在这里

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; }
        }

}
}

类的每个实例只知道它自己。它不知道“总体”,因此无法计算该值在总体“总体”中所占的百分比

您需要一个类来实际执行这些计算(很像Excel“应用程序”根据单元格内容进行计算)

试试这个:

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
  }
}
Class ProductionManager
{
列出生产项目
添加ProductionItem(ProductionItem)
{
//将生产项目添加到_ProductionItems列表
//现在
//1)枚举_ProductionItems的当前集合
//2)继续运行总计
//3)现在重新枚举_ProductionItems的当前集合
//用其各自占总数的百分比更新每个项目
//您在步骤2中进行了计算。
//并用相应的百分比填充每个ProductionItem
}
}

您必须使用2个类,因为产品存在于细节之外,所以您应该这样做

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();
  }
}
公共类productionDetail
{
公共字符串ProductName{get;set;}
public int ProductionQuantity{get;set;}
公共双生产百分比{get;set;}
公共产品详细信息(字符串productName,int productQuantity)
{
ProductName=ProductName;
ProductionQuantity=productQuantity;
ProductionPercentage=0;//这将在以后更改
}
}
公营制作
{
公共列表详细信息{get;set;}
公共int TotalProduction{get;set;}
公共制作(列表详细信息)
{
详细信息=我的详细信息;
重新计算百分比();
}
//这里将计算总数
公共void MakeTotal()
{
var总产量=0;
foreach(详细生产详图d)
{
总产量+=d.生产数量;
}
总产量=总产量;
}
公共空间重新计算百分比()
{   
MakeTotal();
//在这里,您将更新百分比的详细记录。
foreach(详细生产详图d)
{
d、 ProductionPercentage=换算成双倍(d.ProductionQuantity)/换算成双倍(总产量)*100;
}
}
公共无效添加详细信息(productionDetail详细信息)
{
详细信息。添加(详细信息);
重新计算百分比();
}
}

谢谢。。。这帮了大忙