2

enter image description hereI have a table Questions and Answesrs in my DB, now I need to extract one by one each questions with it own answers, and by pressing the NEXT button to go to the second question.

  {    
  public partial class TakeTest : Form
   {
    public static User CurrentUser { get; set; }
    private DataService dataService;
    private List<Category> categories;
    private List<Exam> tests;
    // private List<Question> questions;
    private Dictionary<Question, List<Answer>> qa;    

    public TakeTest()
    {
        InitializeComponent();
        dataService = new DataService();
    }

    private void TakeTest_Load(object sender, EventArgs e)
    {

        // category
        categories = dataService.GetCategories();

        if (categories.Count > 0)
        {   
            cmbSelCategory.Items.AddRange(categories.ToArray());
            cmbSelCategory.SelectedIndex = 0;
        }

    }

    private void cmbSelCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        Category selectedCategory = (Category)cmbSelCategory.SelectedItem;

        tests = dataService.GetTests(selectedCategory.Id);

        cmbSelTest.Items.Clear();
        if (tests.Count > 0)
        {
            cmbSelTest.Items.AddRange(tests.ToArray());
            cmbSelTest.SelectedIndex = 0;
        }

    }

this is how i fill up category and test

QUESTION :Is using Dictionary 'Question, List'Answer'' a good idea? or how else i can do it

1
  • Hi, I cannot see your images (as I am at work) but assuming your questions are unique then storing them in a Dictionary should be fine. This would associate one question with a list of Answers Commented Mar 8, 2013 at 8:58

1 Answer 1

4

You already seem to have a Question class. Why not add an Answers property to that class and populate both the question details and all answers. Then you could create a List<Question>, which you can iterate from beginning to end.

It's harder to iterate a Dictionary than a List.

Acually, looking at your code more closely, you also seem to have an Exam class - this could contain a list of Questions and each Question could contain a list of Answers.

Sign up to request clarification or add additional context in comments.

2 Comments

i have the List<Question> used some where else... so i can use it here to... and i also tought about what you wrote...i`m just asking to see what would be better
"Acually, looking at your code more closely, you also seem to have an Exam class - this could contain a list of Questions and each Question could contain a list of Answers." - yes that is the idea :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.