c# return concatenation of properties
I have a 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开发者_Go百科; set; }
}
I would like to add a method in the class that will concatenate all the variables like this:
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
...
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)
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)));
}
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();
}
}
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. It cannot exist by itself.
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.
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.
Usage isn't far different either: method: string allData = lab.AllData()
or property string allData = lab.AllData
You might also consider just overriding .ToString() in your class.
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;
}
}
精彩评论