0

There's a class:

public class SampleInformation
{
    public string Nutrient { get; set; }
    public decimal NutrientTotal { get; set; }
    public int NoSamples { get; set; }
    public decimal Average { get; set; }
    public decimal StandardDeviation { get; set; }
    public decimal CoVariance { get; set; }
    public decimal PSD { get; set; }
    public decimal NSD { get; set; }
}

and have created a list of the same. Now while adding values, I would like to have PSD = Average + StandardDeviation and NSD = Average - StandardDeviation

My code goes as:

sampleInfoList.Add(new SampleInformation
            {
                Nutrient = "Dry Matter",
                NoSamples = CalculateSampleCount(dryMatter),
                Average = CalculateAverage(dryMatter),
                StandardDeviation = CalculateStandardDeviation(dryMatter),
                CoVariance = CalculateCoVariance(dryMatter),
                NSD = 0,
                PSD = 0,
                NutrientTotal = 0
            });

How can I have something like

 sampleInfoList.Add(new SampleInformation
            {
                Nutrient = "Dry Matter",
                NoSamples = CalculateSampleCount(dryMatter),
                Average = CalculateAverage(dryMatter),
                StandardDeviation = CalculateStandardDeviation(dryMatter),
                CoVariance = CalculateCoVariance(dryMatter),
                NSD = Average - StandardDeviation,
                PSD = Average + StandardDeviation,
                NutrientTotal = 0
            });

I do not want to create another method and pass the list to calculate NSD and PSD.


Create NSD and PSD as properties:

public class SampleInformation
{
    public string Nutrient { get; set; }
    public decimal NutrientTotal { get; set; }
    public int NoSamples { get; set; }
    public decimal Average { get; set; }
    public decimal StandardDeviation { get; set; }
    public decimal CoVariance { get; set; }
    public decimal PSD { get { return Average + StandardDeviation } }
    public decimal NSD { get { return Average - StandardDeviation } }
}

then it will be calculated for you and you can just add new instance like that:

sampleInfoList.Add(new SampleInformation
    {
        Nutrient = "Dry Matter",
        NoSamples = CalculateSampleCount(dryMatter),
        Average = CalculateAverage(dryMatter),
        StandardDeviation = CalculateStandardDeviation(dryMatter),
        CoVariance = CalculateCoVariance(dryMatter),
        NutrientTotal = 0
    });
4

2 回答 2

1

you may try ;

public class SampleInformation
{
    public string Nutrient { get; set; }
    public decimal NutrientTotal { get; set; }
    public int NoSamples { get; set; }
    public decimal Average { get; set; }
    public decimal StandardDeviation { get; set; }
    public decimal CoVariance { get; set; }
    public decimal PSD { get { return Average + StandardDeviation; } private set { } }
    public decimal NSD { get{ return Average-StandardDeviation;} private set{} }
}
于 2012-05-27T05:57:04.983 回答
1

创建 NSD 和 PSD 作为属性:

public class SampleInformation
{
    public string Nutrient { get; set; }
    public decimal NutrientTotal { get; set; }
    public int NoSamples { get; set; }
    public decimal Average { get; set; }
    public decimal StandardDeviation { get; set; }
    public decimal CoVariance { get; set; }
    public decimal PSD { get { return Average + StandardDeviation } }
    public decimal NSD { get { return Average - StandardDeviation } }
}

然后它将为您计算,您可以像这样添加新实例:

sampleInfoList.Add(new SampleInformation
    {
        Nutrient = "Dry Matter",
        NoSamples = CalculateSampleCount(dryMatter),
        Average = CalculateAverage(dryMatter),
        StandardDeviation = CalculateStandardDeviation(dryMatter),
        CoVariance = CalculateCoVariance(dryMatter),
        NutrientTotal = 0
    });
于 2012-05-27T05:55:42.797 回答