How to print the property values of an object to a file/console ?
-
public class Student
{
int id = 1234;
String name = "gustav";
bool state = true;
}It should print: Student: 1234 gustav true
The Student class should NOT CARE AT ALL about outputting anything to the console or other output. It should be just limited to managing the data for a single Student. One problem I see if your Student class should expose its data in public properties, not fields. Having said that, whatever UI code is using this class in some way would be responsible for the presentation. To get the properties in the format you specified in your question, the easiest way to return the string would be to override the Student class ToString method and build the formatted string to return to the caller:
public class Student
{
public int ID { get; set; } = 1234;
public string Name { get; set; } = "gustav";
public bool State { get; set; } = true;
.
.
.
public override string ToString()
{
string result = $"{ID}\r\n{Name}\r\n{State}";return result; }
}
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak
-
The Student class should NOT CARE AT ALL about outputting anything to the console or other output. It should be just limited to managing the data for a single Student. One problem I see if your Student class should expose its data in public properties, not fields. Having said that, whatever UI code is using this class in some way would be responsible for the presentation. To get the properties in the format you specified in your question, the easiest way to return the string would be to override the Student class ToString method and build the formatted string to return to the caller:
public class Student
{
public int ID { get; set; } = 1234;
public string Name { get; set; } = "gustav";
public bool State { get; set; } = true;
.
.
.
public override string ToString()
{
string result = $"{ID}\r\n{Name}\r\n{State}";return result; }
}
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak
Hello, thank you for the reply. The underlying problem ist the following. I want to save the property values of the objects I use in a file and I also want to be able to read it back from that file so that I have a kind of settings file for my app. For logging purposes I also want to be able to print the values to console or file. My understanding was that with serialization this could work.
-
Hello, thank you for the reply. The underlying problem ist the following. I want to save the property values of the objects I use in a file and I also want to be able to read it back from that file so that I have a kind of settings file for my app. For logging purposes I also want to be able to print the values to console or file. My understanding was that with serialization this could work.
Convert the private fields into public properties and you should be able to serialize these values as you see fit.
-
Hello, thank you for the reply. The underlying problem ist the following. I want to save the property values of the objects I use in a file and I also want to be able to read it back from that file so that I have a kind of settings file for my app. For logging purposes I also want to be able to print the values to console or file. My understanding was that with serialization this could work.
This should have been in your original question. What you're looking for is called "serialization". This is the process by which an object is written to a form that can transmitted and read by a matched deserializer and can be accomplished using JSON, XML, and Binary serializers. My previous answer still stands for most serialization operations, you still need the public properties, but you do not need to override the ToString method. You can read up on it at Serialization - .NET | Microsoft Learn[^]
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak