1

Not quite sure what I'm doing wrong. The errors I'm getting are:

The name '_questions' does not exist in the current context - FormChoose.cs

Code:

FormChoose.cs

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace WindowsFormsApplication1
{
    public partial class FormChoose : Form
    {
        public FormChoose()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _questions = GetQuestions("1");
        }
    }
}

Form1.cs

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.Serialization;

Any input or answers on how to achieve this will be greatly appreciated. Possible reward/incentive for anyone that can help out with ltn's post so I can get it working.

Thank you.

2
  • 1
    There is a missing } after button1_click (Form1.cs).
    – Alina B.
    Commented Mar 6, 2013 at 20:05
  • @AlinaB. I tried adding in the missing } and it threw up a load of build errors. Commented Mar 7, 2013 at 14:07

4 Answers 4

4

You're trying to access members and methods from a location where they aren't declared. The method and member you're trying to access (_questions and GetQuestions()), are part of Form1, and you're trying to access them in FormChoose. The only way to do that, is to have a reference to a Form1 object in FormChoose. And I'm not really seeing anywhere where you are declaring what path is supposed to be in Form1.

4
  • Forgive me if this is a silly question, but how do I make a reference to an object in another form? I changed the path to "quiz.xml" and the error is gone. Commented Mar 6, 2013 at 20:14
  • There are a few ways to do it. I would suggest you start by reading about Classes. And follow on that article to anything else that you don't understand.
    – PiousVenom
    Commented Mar 6, 2013 at 20:17
  • 1
    Sharing this kind of logic between forms can lead to scalability issues. For starters, I would look into encapsulating all your saving and retrieval of questions in a new controller'esque class that simply defines a get and save for questions. If you create a new static class that has a private List<Question> property and a public List<Question> GetQuestionsByDifficulty() then all your forms can access the controller class and you can either load your data up front or as needed.
    – Ross Bush
    Commented Mar 6, 2013 at 20:37
  • @ltn Could you give me an example of what you mean if you don't mind? I understand how to create a static class but not quite sure in this instance what I should be putting between it - Am I relocating code or typing new code in? Is there a way we could chat / message privately? Commented Mar 6, 2013 at 20:45
1

in addition to the answer from CL4pTR4P, you have this:

private List<Question> GetQuestions(string difficulty)
{
    var quiz = XDocument.Load(path);

but path isn't declared anywhere in your code, which is why you are getting The name 'path' does not exist in the current context - Form1.cs

You need to declare it and set it to a suitable value

3
  • I changed it to XDocument.Load("quiz.xml") and the error is gone. Is that correct? Commented Mar 6, 2013 at 20:12
  • I don't know where quiz.xml resides - you might want the full path Load(@"fullpath\quiz.xml");
    – NDJ
    Commented Mar 6, 2013 at 20:15
  • quiz.xml is in a folder with all of the other .cs and .resx files Commented Mar 6, 2013 at 20:18
0

The attributes _questions and GetQuestions are declared in Form1 and not in formChoose.

0

1 - Create a new class to place your question repository.

namespace WindowsFormsApplication1
{
    public class QuestionController
    {
        private static List<Question> _questions = new List<Question>();

        public static void LoadData(string path)
        {
            //Load Data from path->_questions
        }

        public static List<Question> GetQuestionsByDifficulty(int difficulty)
        {
            return _questions.Where(p => p.Difficulty == difficulty).ToList();
        }
    }
}

2 - Load your repository in application startup code. In your case:

public Form1()
{
    InitializeComponent();
    QuestionController.LoadData("");
}

You should be able to access the question controller methods whenever it is in scope.

4
  • Thank you, although I'm not sure what code goes in the LoadData part? I feel horrible asking a lot of things but I'm really quite stuck as I've never dealt with this sort of stuff before. Thank you for responding though. Commented Mar 6, 2013 at 22:18
  • I have the code in a .zip if it will help you understand my confusion? I'd greatly appreciate if you could help out as it is the last major stumbling block I have in my project. Commented Mar 6, 2013 at 23:19
  • If anyone could add to this or help out that'd be great. Possibly a reward will be given if I get it working. Commented Mar 7, 2013 at 14:10
  • please email me at [email protected] if you have the time Commented Mar 7, 2013 at 22:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.