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
?
views:
46answers:
3
+1
A:
Collection classes are defined as part of the System.Collections
or System.Collections.Generic
namespace.
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
2010-05-16 06:55:38
Thanks for help. Should be something like this.
dcmovva
2010-05-16 07:10:58
+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
2010-05-16 06:58:11
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
2010-05-16 07:08:53
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
2010-05-16 07:01:04