Skip to main content

How to: Use complex objects as token values

  • 2 minutes to read

The code below illustrates how to override the ToString() method for complex objects, stored as the TokenEditToken values.

//#1
//Override the ToString() method
public class Customer {
    public int ID { get; set; }
    public string Name { get; set; }

    public Customer(Int32 id, string name) {
        this.ID = id;
        this.Name = name;
    }

    public override string ToString() {
        return ID.ToString();
    }
}


public partial class MainForm : XtraForm {
    //...
        tokenEdit1.Properties.Tokens.Add(new TokenEditToken("Mary", new Customer(1, "Mary")));
        tokenEdit1.Properties.Tokens.Add(new TokenEditToken("John", new Customer(2, "John")));
        tokenEdit1.Properties.Tokens.Add(new TokenEditToken("David", new Customer(3, "David")));
}

//#2
//Assign a unique token value
public class Customer {
    public int ID { get; set; }
    public string Name { get; set; }
    }
}

public partial class MainForm : XtraForm {
    //...
        List<Customer> Customers = new List<Customer>();
        Customers.Add(new Customer() { ID = 1, Name = "John" });
        Customers.Add(new Customer() { ID = 2, Name = "Mary" });
        Customers.Add(new Customer() { ID = 3, Name = "David" });
        foreach (Customer customer in Customers) {
            tokenEdit1.Properties.Tokens.Add(new TokenEditToken(customer.Name, customer.ID));
        }
    }
}