override string ToString()

4 posts, 1 answers
  1. Vitaliy
    Vitaliy avatar
    36 posts
    Member since:
    Dec 2008

    Posted 21 Nov 2014 Link to this post

    Most of the GUIs uses the ToString() method to represent an object on the view.

    E.g.
    var context = new EntitiesModel();
    companyTypeComboBoxEdit.Properties.Items.AddRange(context.ContactTypes);

    Unfortunately it will show a list of this by default:
    DataAccessModel.Contacts.CompanyType
    DataAccessModel.Contacts.CompanyType
    DataAccessModel.Contacts.CompanyType
    DataAccessModel.Contacts.CompanyType
    DataAccessModel.Contacts.CompanyType


    I will need to do this:

    namespace DataAccessModel.Contacts
    {
        public partial class CompanyType
        {
            public override string ToString(){
                return _companyTypeName;
            }
        }
    }  

    To get the required list:

    • Public Limited Company
    • Limited Company
    • Sole Trader
    • Limited Liability Partnership
    • Partnership

    Of course I can use an essential attribute instead of the overridden method there in that example.
    But sometime it would be difficult to specify this, especially in a labyrinth of some GUI libraries. It can simply takes a call to the ToString() without any chance to specify the member.

    So I have many files like that.
    Or like this:

    namespace DataAccessModel.Contacts
    {
        public partial class NewCompany
        {
            public override string ToString(){
                return _newCompanyName;
            }
        }
    }

    Maybe we can somehow tick in the designer a string field to specify that we want to see that field as essential, and make the ToString() implementation automatically?  

    Many thanks

  2. Kaloyan Nikolov
    Admin
    Kaloyan Nikolov avatar
    118 posts

    Posted 25 Nov 2014 Link to this post

    Hello Vitaliy,

    The right approach in this case would be to leverage the binding capabilities provided by the particular GUI library rather than using the ToString() overload.

    An example for WPF binding is (taken from this article):
    <telerik:RadGridView x:Name="radGridView"
                             AutoGenerateColumns="False">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding FirstName}" Header="First Name" />
                </telerik:RadGridView.Columns>
    </telerik:RadGridView>

    with the native GUI binding you can achieve different formatting, coloring, etc.. which is not possible with the ToString way. 

    I hope this helps.

    Regards,
    Kaloyan Nikolov
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
  4. Vitaliy
    Vitaliy avatar
    36 posts
    Member since:
    Dec 2008

    Posted 25 Nov 2014 in reply to Kaloyan Nikolov Link to this post

    Kaloyan,

    I don't like WPF so much like you. Probably will read your article later. :-)

    Yes, GUI allows to specify many visible attributes of persistent object value of what you need to show to the user.
    But often all that you need is just a text....

    Well..., I use DevExpress win forms (and your ORM) in years. But I guess it works similarly in most of GUI libraries.

    Imagine:
    A grid. You're going to place a list of your persistent objects into there.
    Some required public properties of your object will be there as columns.
    However some properties of that properties of complex types, They reference to other persistent objects.
    You will see DataAccessModel.Contacts.CompanyType there in that columns. Because the GUI will simply call the ToString(). 
    If we will think about a common object that could be used in 5 more places on the GUI. (Maybe in lists, combo-boxes,..., different forms/frames/windows), then it will be obvious - you won't like to specify the same in a GUI designer five more times to show the same text in five more places. So, we will need to use some other approach (e.g. like described ^^^)

    Or if we could specify the ToString() then all the GUI(es) will simply show that what we need.

    Actually I have no issue with it. Or I would use another section of the forum..
    I just want to make you software better, because I use it. (And maybe somebody uses too)
  5. Answer
    Kaloyan Nikolov
    Admin
    Kaloyan Nikolov avatar
    118 posts

    Posted 27 Nov 2014 Link to this post

    Hello Vitaliy,

    You are right in that case this a good approach. So you should either use the ToString() method or a special DisplayProperty which will do the same thing. 

    Unfortunately at the moment we do not have out-of-the box way for defining such behavior through the designer. 

    Thank you reporting us this potential feature. 

    Regards,
    Kaloyan Nikolov
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top