【问题标题】:Constructor with argument :Missing return int value带参数的构造函数:缺少返回 int 值
【发布时间】:2014-11-11 13:37:56
【问题描述】:

我有一个包含许多属性的类:

public class Report
{
    public int reportId { get; set; }
    public string sentDate { get; set; }
    public string photoName { get; set; }
    public string state { get; set; }
    public string patientName { get; set; }
    public string doctorName { get; set; }
    public string sex { get; set; }
    public int isPregnant { get; set; }
    public float weight { get; set; }
    public float height { get; set; }
    public int age { get; set; }
    public string information { get; set; }
    public string response { get; set; }

然后我用参数实现一个构造函数:

public Report(int id, string photoName, string sentDate, string state,string response)
{
    this.reportId = id;
    this.photoName = photoName;
    this.sentDate = sentDate;
    this.state = state;
    this.response = response;
}

最后我实现了调用这个构造函数的方法:

 public static List<Report> GetListReportByEmail(string email)
{      
    DataTable dt = SqlHelper.ExecuteDataset(
        new SqlConnection(Tls.ConnStr), 
        "spReportGetByEmail", 
        email).Tables[0];
    List<Report> tempItems = new List<Report>();
    if (dt.Rows.Count != 0)
    {

        foreach (DataRow rw in dt.Rows)
        {
            tempItems.Add(
                new Report(
                    int.Parse(rw["ReportId"].ToString()), 
                    Tls.GetBasicUrl() + "/Zdnn000kjUpload/__zd__MedicalConsultation/" 
                        + rw["PhotoName"].ToString(), 
                    DateTime.Parse(rw["SentDate"].ToString()).ToString("dd/M/yyyy"), 
                    rw["State"].ToString(), 
                    rw["Response"].ToString()));
        }
        return tempItems;
    }
    else
    {
        return null;
    }
}

但是当我检查返回值时,除了构造函数中提到的属性之外,我还发现了其类型为 intfloat 且值为 0 的所有属性。

我需要这种方法用于 Web 服务。当我测试这个网络服务时,我发现构造函数中提到的属性,还有 intfloat 没有填充:

<Report>
<reportId>4</reportId>
<sentDate>21/6/2014</sentDate>
<photoName>
http://localhost:2055/Zdnn000kjUpload/__zd__MedicalConsultation/
</photoName>
<state/>
<isPregnant>0</isPregnant>
<weight>0</weight>
<height>0</height>
<age>0</age>

<response/>
</Report>

知道存储过程没问题。

所以谁能帮我只传输构造函数中提到的变量:我不需要传输age,height,weight,isPregnant

谢谢。

【问题讨论】:

  • 那么您的构造函数只设置了一些属性 - 您希望填充其余的属性是什么?
  • 所见即所得。其他属性没有更新,所以它们是 0。到底是什么问题?
  • 用这个方法我只需要填充这些属性,其他的不会填充,但是我在这个方法的返回中找到了。
  • 我认为@Himitcho 希望未填充的属性包含在 xml 中。我在想,为了实现这一点,您可能需要使用 Nullable 作为属性类型。例如public float? weight { get; set; }.

标签: c# asp.net xml


【解决方案1】:

您的构造函数只分配以下内容:

this.reportId = id;
this.photoName = photoName;
this.sentDate = sentDate;
this.state = state;
this.response = response;

您为什么希望其他属性(isPregnantweightheightage 等)也会被填充?您不会在任何时候分配这些属性值。

【讨论】:

猜你喜欢
  • 2020-07-08
  • 2019-05-14
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 2012-08-07
相关资源
最近更新 更多