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:

Image

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: 
First Name - {0} 
Last Name - {1} 
Address - {2} 
Phone - {3}", FirstName, LastName, Address, Phone);
    }
}
Public Class Customer
    Public Sub New(firstName__1 As String, lastName__2 As String, address__3 As String, phone__4 As String)
        FirstName = firstName__1
        LastName = lastName__2
        Address = address__3
        Phone = phone__4
    End Sub

    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set
            m_FirstName = Value
        End Set
    End Property
    Private m_FirstName As String
    Public Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set
            m_LastName = Value
        End Set
    End Property
    Private m_LastName As String
    Public Property Address() As String
        Get
            Return m_Address
        End Get
        Set
            m_Address = Value
        End Set
    End Property
    Private m_Address As String
    Public Property Phone() As String
        Get
            Return m_Phone
        End Get
        Set
            m_Phone = Value
        End Set
    End Property
    Private m_Phone As String

    Public Overrides Function ToString() As String
        Return [String].Format("Customer Details: " & vbLf & "First Name - {0} " & vbLf & "Last Name - {1} " & vbLf & "Address - {2} " & vbLf & "Phone - {3}", FirstName, LastName, Address, Phone)
    End Function
End Class

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:

Image