Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 在无参数构造函数内设置属性_C#_Linq_Parameters_Constructor - Fatal编程技术网

C# 在无参数构造函数内设置属性

C# 在无参数构造函数内设置属性,c#,linq,parameters,constructor,C#,Linq,Parameters,Constructor,我使用linq通过无参数构造函数填充对象。我在构造函数上设置了一个断点,并注意到没有填充“this”的值。我想要的一个属性是其他属性的总和。下面是一个简单的示例,展示了我的类的外观: // I'd like totalEstimatedUse to be the total of estimatedType1 + estimatedType2 public class EstimatedUse { public string ID { get; set; } public dou

我使用linq通过无参数构造函数填充对象。我在构造函数上设置了一个断点,并注意到没有填充“this”的值。我想要的一个属性是其他属性的总和。下面是一个简单的示例,展示了我的类的外观:

// I'd like totalEstimatedUse to be the total of estimatedType1 + estimatedType2
public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }
    public double totalEstimatedUse { get; set; } // I'd like this to be the sum
}

    // parameterless constructor - I cannot get anything to work inside this because
    // 'this' has no values when it's populated (from linq in my code)
    public EstimatedUse
    {
    }

    // here is a constructor that has parameters
    public EstimatedUse(string id, double estimatedType1, double estimatedType2)
    {
        this.ID = id;
        this.estimatedType1 = estimatedType1;
        this.estimatedType2 = estimatedType2;
        this.totalEstimatedUse = estimatedType1 + estimatedType2;
    }
出现的问题是,我正在使用linq填充它,它将进入无参数构造函数,而这不会设置我的totalEstimatedUse。我要做的是使用带参数的构造函数再次设置它。有没有比我下面所说的更合适的方法来实现这一点

EstimatedUse estimatedUse = null;

// cutoff linq to keep example clean
.Select(e => new EstimatedUse
{
    ID = e.ProjectID,
    estimatedType1 = e.estimatedType1),
    estimatedType1 = e.estimatedType2),
}).FirstOrDefault();

// below is to set the length but I wish I could set in the parameterless constuctor if    
// it's possible    
estimatedUse = new EstimatedUse(estimatedUse.ID, estimatedUse.estimatedType1,
                                stimatedUse.estimatedType2);

您没有在构造函数中设置值,而是使用对象初始值设定项语法。该语法通过调用对象的构造函数来创建对象,然后设置指定的属性

要使用这些值执行对象的初始化,实际上需要有一个接受它们的构造函数


当然,另一种选择是避免首先初始化对象。在这种特殊情况下,这意味着不在字段中保存总和,而是在属性getter中根据请求计算总和。

您没有在构造函数中设置值,而是使用对象初始值设定项语法。该语法通过调用对象的构造函数来创建对象,然后设置指定的属性

要使用这些值执行对象的初始化,实际上需要有一个接受它们的构造函数


当然,另一种选择是避免首先初始化对象。在这种特殊情况下,这意味着不在字段中保存总和,而是在属性getter中根据请求计算总和。

您没有在构造函数中设置值,而是使用对象初始值设定项语法。该语法通过调用对象的构造函数来创建对象,然后设置指定的属性

要使用这些值执行对象的初始化,实际上需要有一个接受它们的构造函数


当然,另一种选择是避免首先初始化对象。在这种特殊情况下,这意味着不在字段中保存总和,而是在属性getter中根据请求计算总和。

您没有在构造函数中设置值,而是使用对象初始值设定项语法。该语法通过调用对象的构造函数来创建对象,然后设置指定的属性

要使用这些值执行对象的初始化,实际上需要有一个接受它们的构造函数


当然,另一种选择是避免首先初始化对象。在这种特殊情况下,这意味着不在字段中保存总和,而是在属性getter中根据请求计算总和。

totalEstimatedUse
属性更改为自动对值求和的只读属性可能会更好

public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }

    public double totalEstimatedUse
    {
        get { return this.estimatedType1 + this.estimatedType2; }
    }

    // ...
}

这样,您不必在创建时设置属性的值,它将与您的其他值保持最新。

totalEstimatedUse
属性更改为自动求和的只读属性可能会更好

public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }

    public double totalEstimatedUse
    {
        get { return this.estimatedType1 + this.estimatedType2; }
    }

    // ...
}

这样,您不必在创建时设置属性的值,它将与您的其他值保持最新。

totalEstimatedUse
属性更改为自动求和的只读属性可能会更好

public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }

    public double totalEstimatedUse
    {
        get { return this.estimatedType1 + this.estimatedType2; }
    }

    // ...
}

这样,您不必在创建时设置属性的值,它将与您的其他值保持最新。

totalEstimatedUse
属性更改为自动求和的只读属性可能会更好

public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }

    public double totalEstimatedUse
    {
        get { return this.estimatedType1 + this.estimatedType2; }
    }

    // ...
}

这样,您不必在创建时设置属性的值,它将与您的其他值保持最新。

您也可以更改
totalEstimatedUse
属性的getter,以便在访问它时自动计算总和:

public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }
    public double totalEstimatedUse { get { return estimatedType1 + estimatedType2; }
}

您还可以更改
totalEstimatedUse
属性的getter,以便在访问时自动计算总和:

public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }
    public double totalEstimatedUse { get { return estimatedType1 + estimatedType2; }
}

您还可以更改
totalEstimatedUse
属性的getter,以便在访问时自动计算总和:

public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }
    public double totalEstimatedUse { get { return estimatedType1 + estimatedType2; }
}

您还可以更改
totalEstimatedUse
属性的getter,以便在访问时自动计算总和:

public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }
    public double totalEstimatedUse { get { return estimatedType1 + estimatedType2; }
}

如果您不想在构造函数中传递任何参数,那么应该将应该传递给构造函数的字段设置为全局字段。并在调用该类之前设置该值。然后在构造函数中可以初始化它


虽然这不是一个理想的方法,但这仍然是一个选择。但是您应该看看是否可以将参数传递给构造函数来设置值。

如果您不想在构造函数中传递任何参数,那么您应该将应该传递给构造函数的字段设置为全局。并在调用该类之前设置该值。然后在构造函数中可以初始化它

public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }

    public double totalEstimatedUse
    {
        get { return this.estimatedType1 + this.estimatedType2; }
    }

    // ...
}

虽然这不是一个理想的方法,但这仍然是一个选择。但是您应该看看是否可以将参数传递给构造函数来设置值。

如果您不想在构造函数中传递任何参数,那么您应该将应该传递给构造函数的字段设置为全局。并在调用该类之前设置该值。然后在构造函数中可以初始化它

public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }

    public double totalEstimatedUse
    {
        get { return this.estimatedType1 + this.estimatedType2; }
    }

    // ...
}

虽然这不是一个理想的方法,但这仍然是一个选择。但是您应该看看是否可以将参数传递给构造函数来设置值。

如果您不想在构造函数中传递任何参数,那么
public class EstimatedUse
{
    public string ID { get; set; }
    public double estimatedType1 { get; set; }
    public double estimatedType2 { get; set; }

    public double totalEstimatedUse
    {
        get { return this.estimatedType1 + this.estimatedType2; }
    }

    // ...
}