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:

[C#]
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);
    }
}
[Visual Basic]
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:

<< PreviousNext >>