0

I have this code in the main window class where I declare some values to put in a ListForTesting list and make the tests appear in a combobox. Later there is another combobox that depends on the first on which I present you the code down bellow:

It may be worth to mention that I am completely new to C#. Only coded in VBA. I'm a mechanical engineer, not software :)

Main Window code

public MainWindow()
{
    InitializeComponent();
    grid_projectConfig.Visibility = Visibility.Collapsed;
    grid_projectOverview.Visibility = Visibility.Collapsed;
    grid_test.Visibility = Visibility.Collapsed;
    grid_reports.Visibility = Visibility.Collapsed;

    //ListForTesting holds the hardcoded set of tests and manoeuvres
    List<HelpClass.TestID> ListForTesting = new List<HelpClass.TestID>();

    //TestObject holds the test name, ID and all the manoeuvres related to it
    HelpClass.TestID TestObject = new HelpClass.TestID();
    TestObject.testName = "Steady State";
    TestObject.ID = 0;
    //Manoeuvre holds the manoeuvre name and its ID
    TestObject.Manoeuvres = new List<HelpClass.ManoeuvresID>();
    HelpClass.ManoeuvresID Manoeuvre = new HelpClass.ManoeuvresID();
    Manoeuvre.manName = "30 kph";
    Manoeuvre.manID = 0;
    //add the Manoeuvre to the TestObject
    TestObject.Manoeuvres.Add(Manoeuvre);
    //create new Manoeuvre
    Manoeuvre = new HelpClass.ManoeuvresID();
    Manoeuvre.manName = "50 kph";
    Manoeuvre.manID = 1;
    TestObject.Manoeuvres.Add(Manoeuvre);
    //add the TestObject to the ListForTesting
    ListForTesting.Add(TestObject);


    //display the tests in a combobox
    combobox_testType.ItemsSource = ListForTesting.Select(t => t.testName);
}

Second combobox code

public void combobox_testType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    combobox_testType.ItemsSource = ListForTesting[1].Manoeuvres.Select(t => t.manName);
}

this last line of code does not work because it tells me that it does not exist in the current context.

1
  • What's the question? Commented May 31, 2016 at 11:00

2 Answers 2

3

The problem is the scope of the variable ListForTesting: by declaring it inside your MainWindow constructor you limit its visibility to that method.

To allow to access ListForTesting from another method in your class (i.e.: combobox_testType_SelectionChanged) you have to declare it as a class-level variable like so:

public class MainWindow : Window
{
    private List<HelpClass.TestID> ListForTesting; // Variable declaration

    public MainWindow()
    {
        // code

        ListForTesting = new List<HelpClass.TestID>(); // Initialization

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

Comments

1

the variable ListForTesting is in the scope of the constructor. You need to move the variable out of there for it to be accessible elsewhere in the class.

List<HelpClass.TestID> ListForTesting;

public MainWindow()
{
...
   ListForTesting = new List<HelpClass.TestID>();
...
}

Comments

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.