When you convert an instance of a type (except string
) to a string, such as when you:
txtOutput.Text += "\r\n" + t;
the CLR (.NET runtime) will call the method ToString()
on the passed object. This is a method that all types derived from System.Object
(there are very few types in .NET not derived from Object
) inherit.
But the default implementation just returns the name of the type.
You need to override Object.ToString()
in your type. and return a more meaningful string.
Eg.
public class Transaction {
//...
public override string ToString() {
// Guess field names from constructor:
// new Transaction(reader.GetInt32(0), reader.GetDateTime(1), reader.GetString(2), reader.GetString(3), reader.GetDouble(4))
return String.Format("#{0}: {1} {2} {3} {4} {5}", id, timestamp, string1, string2, number);
}
// ...
Ideally an overflow talking an IFormatProvider
and passing that through to formatting functions should also be present (and will be used by String.Format
and such methods if available). Even better to implement IFormattable
.