c#返回属性的串联

本文关键字:属性 返回 | 更新日期: 2023-09-27 18:02:39

我有一个类:

public class LabOccurrenceForm
{          
    public DateTime Occurrence_Date { get; set; }
    public string Cup_Type { get; set; }
    public string Analytical_Testing_Phase { get; set; }
    public string Area { get; set; }
    public string Preanalytical_Before_Testing { get; set; }
    public string Postanalytical_After_Testing { get; set; }
    public string Other { get; set; }
    public string Practice_Code { get; set; }
    public string Comments { get; set; }
}

我想在类中添加一个方法,将所有变量连接起来,如下所示:

public string AllData
{
    return Occurrence_Date.ToString() + " " +
    Cup_Type + " " +
    Analytical_Testing_Phase + " " +
    Area + " " +
    Preanalytical_Before_Testing + " " +
    Postanalytical_After_Testing + " " +
    Other + " " +
    Practice_Code + " " +
    Comments;
}

这不起作用,因为它想要一个获取或设置。我做错了什么?我该怎么解决这个问题?

c#返回属性的串联

所以,让它成为一个属性,并给它一个get。。。

public string AllData
{
    get
    {
        return Occurrence_Date.ToString() + " " +
        Cup_Type + " " +
        Analytical_Testing_Phase + " " +
        Area + " " +
        Preanalytical_Before_Testing + " " +
        Postanalytical_After_Testing + " " +
        Other + " " +
        Practice_Code + " " +
        Comments;
    }
}

或者让它成为一种方法。。。

public string AllData()
{
    return Occurrence_Date.ToString() + " " +
    Cup_Type + " " +
    Analytical_Testing_Phase + " " +
    Area + " " +
    Preanalytical_Before_Testing + " " +
    Postanalytical_After_Testing + " " +
    Other + " " +
    Practice_Code + " " +
    Comments;
}

或者替代ToString()(在这种情况下这是有意义的(

public override string ToString()
{
    return Occurrence_Date.ToString() + " " +
    Cup_Type + " " +
    Analytical_Testing_Phase + " " +
    Area + " " +
    Preanalytical_Before_Testing + " " +
    Postanalytical_After_Testing + " " +
    Other + " " +
    Practice_Code + " " +
    Comments;
}

由于您想要所有属性-使用反射更简洁:

public string GetAllData()
{
    return string.Join(" ", typeof(LabOccurrenceForm).GetProperties().Select(x => x.GetValue(this, null)));
}

要获得更好的性能,请尝试:

public string AllData
{
    get
    {
        return string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8}",
          Occurrence_Date.ToString(),
          Cup_Type,
          Analytical_Testing_Phase,
          Area,
          Preanalytical_Before_Testing,
          Postanalytical_After_Testing,
          Other,
          Practice_Code,
          Comments);
    }
}

public string AllData
{
    get
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat({0} ", Occurrence_Date.ToString());
        sb.AppendFormat({0} ", Cup_Type);
        sb.AppendFormat({0} ", Analytical_Testing_Phase);
        sb.AppendFormat({0} ", Area);
        sb.AppendFormat({0} ", Preanalytical_Before_Testing);
        sb.AppendFormat({0} ", Postanalytical_After_Testing);
        sb.AppendFormat({0} ", Other);
        sb.AppendFormat({0} ", Practice_Code);
        sb.AppendFormat({0}", Comments);
        return sb.ToString();
    }
}
public string AllData
{
    get
    {
        return Occurrence_Date.ToString() + " " +
        Cup_Type + " " +
        Analytical_Testing_Phase + " " +
        Area + " " +
        Preanalytical_Before_Testing + " " +
        Postanalytical_After_Testing + " " +
        Other + " " +
        Practice_Code + " " +
        Comments;
    }
}

您需要在类内的get中使用return。它不可能单独存在。

如果你试图创建一个方法,你必须将其声明为方法:public string AllData()按照你声明的方式,编译器认为你正在尝试创建一个属性,因此它希望你提供一个get{}语句。

在任何一种情况下,这都是一个小的更改——要创建一个方法,只需在方法名称的末尾添加括号即可。如果您想要一个属性,只需在实际代码的开头添加get{,在末尾添加},无论哪种方式都可以。

用法也没有太大区别:方法:string allData = lab.AllData()或属性string allData = lab.AllData

您也可以考虑在类中重写.ToString((。

您应该添加这样的只读字段:

public string AllData
{
    get
    {
        return Occurrence_Date.ToString() + " " +
               Cup_Type + " " +
               Analytical_Testing_Phase + " " +
               Area + " " +
               Preanalytical_Before_Testing + " " +
               Postanalytical_After_Testing + " " +
               Other + " " +
               Practice_Code + " " +
               Comments;
    }
}