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.


Interesting little question. According to the C++11 standard, sect. 21.4.2.9,

basic_string(const charT* s, const Allocator& a = Allocator());

Requires: s shall not be a null pointer.

Since the standard does not ask the library to throw an exception when this particular requirement is not met, it would appear that passing a null pointer provoked undefined behavior.

4

2 に答える 2

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 に答える
1

試すことができます。

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 に答える