This is a migrated thread and some comments may be shown as answers.

override string ToString()

3 Answers 79 Views
Feature Requests
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vitaliy
Top achievements
Rank 1
Vitaliy asked on 21 Nov 2014, 07:18 AM
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

3 Answers, 1 is accepted

Sort by
0
Kaloyan Nikolov
Telerik team
answered on 25 Nov 2014, 11:51 AM
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.
 
0
Vitaliy
Top achievements
Rank 1
answered on 25 Nov 2014, 05:48 PM
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)
0
Accepted
Kaloyan Nikolov
Telerik team
answered on 27 Nov 2014, 01:22 PM
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.
 
Tags
Feature Requests
Asked by
Vitaliy
Top achievements
Rank 1
Answers by
Kaloyan Nikolov
Telerik team
Vitaliy
Top achievements
Rank 1
Share this question
or