Technical Article: Define the Textual Representation in your Classes

TX Text Control Reporting allows the textual formatting to be done in business objects. For example: The ToString() method can be overridden to return a well formatted address block directly from your business object.

Override the ToString method of your classes

Consider a class to represent a Customer:

The following code is the C# implementation of the above class:

public class Customer
	public Customer(string firstName, string lastName, string address, string phone)
			FirstName = firstName;
			LastName = lastName;
			Address = address;
			Phone = phone;

	public string FirstName { get; set; }
	public string LastName { get; set; }
	public string Address { get; set; }
	public string Phone { get; set; }

	public override string ToString()
		return String.Format("Customer Details: \nFirst Name - {0} \nLast Name - {1} \nAddress - {2} \nPhone - {3}", FirstName, LastName, Address, Phone);

TX Text Control MailMerge calls the ToString() method to get the text from your class that returns a pre-formatted string with carriage returns. The following screenshot shows the results of the above-described concept: