C# 类逻辑不返回值
我有一些用C#编写的代码。我的课程如下C# 类逻辑不返回值,c#,class,visual-studio-2012,C#,Class,Visual Studio 2012,我有一些用C#编写的代码。我的课程如下 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DrivetimeFileDump { public class DriveTimeObject { public decimal? _Parts; public decimal? Pa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DrivetimeFileDump
{
public class DriveTimeObject
{
public decimal? _Parts;
public decimal? Parts
{
get { return _Parts; }
set
{
if (value != null)
{
_Parts = value;
}
else
{
_Parts = 0.00M;
}
}
}
public decimal? GetAmountChargedParts(string ComponentStatus)
{
string[] ComponentStatuses = new string[] { "i", "n", "d", "w", "e", "v" };
foreach (var item in ComponentStatuses)
{
if (item == ComponentStatus)
{
return 0;
}
else
{
}
}
return _Parts;
}
我已经取出了工作代码,以便更容易阅读。然后,我使用实体框架收集项目列表
DriveTimeObject DT = new DriveTimeObject();
DateTime today = DateTime.Now;
DataQuery = (from z in dd.viewDriveTimeFileDump_V2 where z.dtmReported <= today select z).ToList();
一切似乎都在流动,但当值被设置时,当我需要将其设置为0.00时,它将被设置为null。我不确定我做错了什么。谢谢。在您的
foreach
块中,您正在为正在创建的DriverTimeObject
设置Parts
,但您似乎从未设置DT.Parts
。因此,每次调用DT.GetAmountChargedParts
,但它找不到匹配的组件状态
,它将返回\u Parts
(对于当前实例:DT),它将始终为空。检查是否为十进制?是null并将其设置为0.00。当我返回_Parts时,它是否应该检查它是否为null,如果为null,它将返回0.00?非常接近。在GetAmountChargedParts
中,您返回的是\u Parts
,而不是.Parts
。在属性.Parts
中,还可以返回未选中的\u Parts
。对于DT
您从不调用setter,因此DT.Parts
和DT.GetAmountChargedParts
始终返回null。
foreach (var item in DataQuery)
{
decimal? AmountChargedParts = DT.GetAmountChargedParts(item.chrComponSts);
DataFile.Add( new DriveTimeObject
{
Parts = AmountChargedParts,
});
}