I have an application where I need to populate a textbox with a company name and I have filled a custom AutoCompleteStringColection with all the available company names from the database. When a user enters changes the company name by typing and selecting from the list a new company name I need to have the id (Guid), of the selected company so I can do a lookup and get the rest of the company information. Because the company name is not guaranteed to be unique I cannot do a lookup on the name and expect to have the right record. I looked at extending the string class, but all I can find are examples that add methods. I tried that by adding a variable to store the id and methods to get and set the id, but when retrieving the id it is always the last id set. Can a property be added to a class by extending it? I have already changed what I was trying to do to do a lookup on the company name and display a list the user will choose from if multiple matches are returned, but I would still like to know if I can add a property this way in case it comes up again.
No, you cannot extend classes with properties. Additionally, String
is sealed
so you can't extend it by inheriting. The only recourse is to composition: encapsulate string
in your own class.
It sounds like you should create your own class:
class Company {
public string Name {get;set;}
public override string ToString() {return Name;}
// etc
}
Now bind to a set of Company
objects; the ToString
override will ensure that the Name
is displayed by default, and you can add whatever else you need. For more complex scenarios, you can use (for example) DisplayMember
and ValueMember
(of a combo-box) to point at different properties (rather than the default ToString
).
You should use a ComboBox rather than a TextBox. Create a custom type that has your company name and id in it, making sure that it overrides ToString to return the company name. Add those custom types to the ComboBox rather than straight-up strings, and use AutoCompleteSource of ListItems.
I used Konrad's answer and for the sake of completeness I am posting my solution here. I needed to show my user an autocomplete list of company names, but since they could have multiple companies with the same name I needed the Guid id to find their choice in the database. So I wrote my own class inheriting from AutoCompleteStringCollection.
public class AutoCompleteStringWithIdCollection : AutoCompleteStringCollection
{
private List<Guid> _idList = new List<Guid>();
/*-- Properties --*/
public Guid this[int index]
{
get
{
return _idList[index];
}
}
public Guid this[string value]
{
get
{
int index = base.IndexOf(value);
return _idList[index];
}
}
/*-- Methods --*/
public int Add(string value, Guid id)
{
int index = base.Add(value);
_idList.Insert(index, id);
return index;
}
public new void Remove(string value)
{
int index = base.IndexOf(value);
if (index > -1)
{
base.RemoveAt(index);
_idList.RemoveAt(index);
}
}
public new void RemoveAt(int index)
{
base.RemoveAt(index);
_idList.RemoveAt(index);
}
public new void Clear()
{
base.Clear();
_idList.Clear();
}
}