简体   繁体   English

c# 返回串联属性

[英]c# return concatenation of properties

I have a class:我有一个 class:

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; }
}

I would like to add a method in the class that will concatenate all the variables like this:我想在 class 中添加一个方法,它将像这样连接所有变量:

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

This did not work because it wants a get or set.这不起作用,因为它需要获取或设置。 What am I doing wrong?我究竟做错了什么? How can I fix this?我怎样才能解决这个问题?

So, make it a property and give it a get ...所以,让它成为一个属性,并给它一个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;
    }
}

Or make it a method...或者让它成为一种方法......

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

Or override ToString() instead (which sort of makes sense in this context)或者改写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;
}

Since you want all properties - more succinct using reflection:由于您想要所有属性 - 使用反射更简洁:

public string GetAllData()
{
    return string.Join(" ", typeof(LabOccurrenceForm).GetProperties().Select(x => x.GetValue(this, null)));
}
public string AllData
{
    get
    {
        return Occurrence_Date.ToString() + " " +
        Cup_Type + " " +
        Analytical_Testing_Phase + " " +
        Area + " " +
        Preanalytical_Before_Testing + " " +
        Postanalytical_After_Testing + " " +
        Other + " " +
        Practice_Code + " " +
        Comments;
    }
}

You need to use return in a get within a class.您需要在 class 中的 get 中使用 return。 It cannot exist by itself.它不能单独存在。

for better performance, try:为了获得更好的性能,请尝试:

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);
    }
}

or或者

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();
    }
}

If you're trying to make a method, you have to declare it as a method: public string AllData() They way you've declared it, the compiler thinks you're trying to create a property, thus it wants you provide a get{} statement.如果你试图创建一个方法,你必须将它声明为一个方法: public string AllData()你声明它的方式,编译器认为你正在尝试创建一个属性,因此它希望你提供一个get{}语句。

In either case, it's a minor change- to make a method, just add your parentheses at the end of the method name.在任何一种情况下,这都是一个微小的变化——创建一个方法,只需在方法名称的末尾添加括号即可。 If you want a property, just add get{ at the beginning and } at the end of your actual code and you'll be fine either way.如果您想要一个属性,只需在实际代码的开头添加get{并在末尾添加} ,无论哪种方式都可以。

Usage isn't far different either: method: string allData = lab.AllData() or property string allData = lab.AllData用法也没有太大区别:方法: string allData = lab.AllData()或属性string allData = lab.AllData

You might also consider just overriding.ToString() in your class.您也可以考虑在 class 中覆盖.ToString()。

You should add the readonly field like this:您应该像这样添加只读字段:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM