tags:

views:

46

answers:

3

I have a requirement to implement some kind of dictionary object. Something like MyDict<K,V1, V2). For example if I have a Question as Key(k) then Correct answer is V1 . V2 is user selected answer. Is there a collection that would satisfy this requirement in C#. If I have to design my own type, what interfaces should I implement. ICollection and Ilist ?

+1  A: 

Collection classes are defined as part of the System.Collections or System.Collections.Generic namespace.

Generic Dictionary Class

something along the lines:

        Dictionary<Question,Answer> qDict = new Dictionary<Question, Answer>();

        Answer attempt_v2 = new Answer();
        Question question = new Question();
        if (qDict.ContainsKey(question))
        {
            Answer actual_answerv1 = qDict[question];

            if (actual_answerv1 == attempt_v2)
            {
                // Answers match
            }
        }

[Note: I just knocked that up. It may not be the best solution to the problem of matching questions and answers...]

Mitch Wheat
Thanks for help. Should be something like this.
dcmovva
+1  A: 

If you are using .NET 4, there are several generic Tuple classes that you can use.

So, you can use:

Dictionary<T,Tuple<V1,V2>>

In earlier versions, you could use KeyValuePair<T1,T2> as the second generic type:

Dictionary<T,KeyValuePair<V1,V2>>
Oded
interesting. There might be multiple right answers in the future too. So I should just create type with out any generics. like class mytype { string Question; IList<string> CorrectAnswers; IList<string> SelectedAnswers;}}
dcmovva
A: 

I think a new implementation is needed.

OR if your Answer has a property Selected you can create.

Dictionary<Question, IList<Answer>>

or u can have a type with

class Answers {
    Ilist Answers;
    Answer Selected;
}
fampinheiro
thanks for response
dcmovva