Igor M
Igor M

Reputation: 474

Using data base and C#

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

Upvotes: 2

Views: 122

Answers (1)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

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.

Upvotes: 4

Related Questions