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.
Share
No, you cannot extend classes with properties. Additionally,
Stringissealedso you can’t extend it by inheriting. The only recourse is to composition: encapsulatestringin your own class.