user2141272
user2141272

Reputation: 297

The name 'xx' does not exist in the current context

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.

Upvotes: 1

Views: 2569

Answers (4)

Ross Bush
Ross Bush

Reputation: 15155

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.

Upvotes: 0

Nonick
Nonick

Reputation: 43

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

Upvotes: 0

NDJ
NDJ

Reputation: 5194

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

Upvotes: 1

PiousVenom
PiousVenom

Reputation: 6908

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.

Upvotes: 4

Related Questions