I would add to the correct Solution 1: this is a bad style of programming; the whole idea of comparison of something in the UI state with some
immediate constants in your code is bad idea and wrong code design.
Such immediate constants as "Alimentos" are referred to as "magic strings". Such programming is not maintainable, which becomes especially obvious if you write the same constant twice. The compiler won't detect any problem is you misspell any of those strings. This is a well-known
anti-pattern. Please see:
http://en.wikipedia.org/wiki/Magic_string_%28programming%29#Magic_strings_in_code[
^].
See also:
http://en.wikipedia.org/wiki/Anti-pattern[
^].
What to do? In worst case, at least declare an explicit constant and do it only once in your whole solution. It should be the only place where the string is declared. These are the fundamental principles:
http://en.wikipedia.org/wiki/Single_Point_of_Truth[
^],
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[
^].
But even you do it, this approach looks way too much ad-hoc. Right approach would be something like this.
Don't put strings in your list boxes, combo boxes and other item controls. Create special types (classes or structures) representing the data you need for your functionality, and add instances of them to your item controls. Then, for example, for comparison, type-cast the items to your types and use your item type's members.
The remaining problem is: what is to be shown in the UI? Simple: in simplest case, whatever its method
ToString()
shows, so you can override
System.Object.ToString()
. Or, better, use
binding, which is a whole big separate topic.
—SA