如何将计算的 属性 值分配给对象?
How to add assign a calculated property value to an object?
我正在将一些属性读回到一个对象的构造函数中。其中一个属性是根据另一个 属性.
计算得出的
但是当我创建这个对象时,计算出的 Implementation_End_String
属性 值总是空的:
private string implementationEndString;
public string Implementation_End_String {
get{
return implementationEndString;
}
set {
implementationEndString= DataTimeExtensions.NullableDateTimeToString(Implementation_End);
}
}
问题:
如何将计算的 属性 传递给对象构造函数?
这是ctor的一个要点和计算属性:
private string implementationEndString;
public string Implementation_End_String {
get{
return implementationEndString;
}
set {
implementationEndString= DataTimeExtensions.NullableDateTimeToString(Implementation_End);
}
}
public DateTime? Implementation_End { get; set; }
public ReleaseStatus(DateTime? implementationEnd)
{
//value is assigned at runtime
Implementation_End = changeRequestPlannedImplementationEnd;
}
这样写。
private string implementationEndString = DataTimeExtensions.NullableDateTimeToString(Implementation_End);
public string Implementation_End_String
{
get{ return implementationEndString; }
set{ implementationEndString=value; }
//if you don't want this property to be not changed, just remove the setter.
}
之后,当您获得 属性 值时,它将采用 DataTimeExtensions.NullableDateTimeToString(Implementation_End);
的值。在您的代码中,当您尝试获取实现 returns null
时,因为 implementationEndString 是 null
.
不需要字段 implementationEndString
。只需将 Implementation_End_String
设为只读 属性 并在需要时从另一个 属性 创建字符串:
public string Implementation_End_String
{
get
{
return DataTimeExtensions.NullableDateTimeToString(Implementation_End);
}
}
我正在将一些属性读回到一个对象的构造函数中。其中一个属性是根据另一个 属性.
计算得出的但是当我创建这个对象时,计算出的 Implementation_End_String
属性 值总是空的:
private string implementationEndString;
public string Implementation_End_String {
get{
return implementationEndString;
}
set {
implementationEndString= DataTimeExtensions.NullableDateTimeToString(Implementation_End);
}
}
问题:
如何将计算的 属性 传递给对象构造函数?
这是ctor的一个要点和计算属性:
private string implementationEndString;
public string Implementation_End_String {
get{
return implementationEndString;
}
set {
implementationEndString= DataTimeExtensions.NullableDateTimeToString(Implementation_End);
}
}
public DateTime? Implementation_End { get; set; }
public ReleaseStatus(DateTime? implementationEnd)
{
//value is assigned at runtime
Implementation_End = changeRequestPlannedImplementationEnd;
}
这样写。
private string implementationEndString = DataTimeExtensions.NullableDateTimeToString(Implementation_End);
public string Implementation_End_String
{
get{ return implementationEndString; }
set{ implementationEndString=value; }
//if you don't want this property to be not changed, just remove the setter.
}
之后,当您获得 属性 值时,它将采用 DataTimeExtensions.NullableDateTimeToString(Implementation_End);
的值。在您的代码中,当您尝试获取实现 returns null
时,因为 implementationEndString 是 null
.
不需要字段 implementationEndString
。只需将 Implementation_End_String
设为只读 属性 并在需要时从另一个 属性 创建字符串:
public string Implementation_End_String
{
get
{
return DataTimeExtensions.NullableDateTimeToString(Implementation_End);
}
}