"Peter Duniho" <(E-Mail Removed)> skrev i meddelandet
news:(E-Mail Removed)...
> On 1/27/11 10:20 PM, Tony Johansson wrote:
>> Hello!
>>
>> There are two listboxes in the first one I have differend kind of animal
>> category such as Bird,Mammal,Reptile and the second one should show all
>> those animal type that correspond to the animal category.
>> An example if I choose Mammal in the category listBox then in the animal
>> type listbox should display Bear,Cat,Deer,Lion,Dog,Horse,Panda and Wolf.
>>
>> So when I choose a category only the animal that belong to the selected
>> animal category should be displayed in the animal type listBox.
>>
>> What is the best kind of solution and what class is most suitable here ?
>
> Impossible to say given your description. A lot of the specifics depend
> on where the data comes from, and how you're using it elsewhere in the
> program.
>
> What I can tell you is that you'll have two ListBox instances (most likely
> added to the parent control or form with the Designer), you'll respond to
> one of the selection-changed events in the first ListBox, and then clear
> and repopulate the second ListBox based on the new selection.
>
> If you are just trying to get the job done, worrying about the _best_ kind
> of solution is not good use of your time. Just get _a_ solution that
> works well enough, and then improve it if necessary.
>
> If you are trying to comply with some certification requirement, exam,
> whatever, then asking the question here is cheating. 
>
> Pete
Here I have a working solution that is ok.
But there is one thing in this solution that I don't like and that is that I
have to use hard coded string for the
animal type class. I can't see any suitable way to find out which class I
have as animal type.
In this example below I have tvo classes for each animal category.
In the Init method I fill the collection with a category and the animal type
that I have as you can see.
In method GetMyValues I pass in the key for my collection and then a return
a List<T> for the values that match the key.
I hate use hard coded string that I have to use here.
This problem has nothing to do with any type of certification.
All certification test is done at a specialist company that is allowed to
give these kind of test.
class Category
{
private NameValueCollection nvc = new NameValueCollection();
public Category()
{
Init();
}
private void Init()
{
foreach (string value in Enum.GetNames(typeof(CategoryType)))
{
if (value == CategoryType.Bird.ToString())
{
nvc.Add(value, "Duck");
nvc.Add(value, "Kolibri");
}
else if (value == CategoryType.Insect.ToString())
{
nvc.Add(value, "Butterfly");
nvc.Add(value, "Bee");
}
else if (value == CategoryType.Mammal.ToString())
{
nvc.Add(value, "Cat");
nvc.Add(value, "Bear");
}
else if (value == CategoryType.Marine.ToString())
{
nvc.Add(value, "Pike");
nvc.Add(value, "GoldFish");
}
else if (value == CategoryType.Reptile.ToString())
{
nvc.Add(value, "Frog");
nvc.Add(value, "Snake");
}
}
}
public List<string> GetMyValues(string key)
{
List<string> returnValueList = new List<string>();
foreach(string s in nvc.GetValues(key))
{
returnValueList.Add(s);
}
return new List<string>(returnValueList);
}
}
//Tony