0

I'm pretty expereinced in java and can't seem to figure out how to use a global variable in a sense.

How would I access the list foodSource in the on selectedIndexChanged method? I want to have it so when an item is selected, its price is displayed in a separate label.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        initFood();
    }

    private void initFood()
    {
        var foodSource = new List<Food>();

        foodSource.Add(new Food { name = "Pizza", price = 10 });
        foodSource.Add(new Food { name = "Burger", price = 15 });
        foodSource.Add(new Food { name = "Chips", price = 5 });
        this.menu.DataSource = foodSource;
        menu.DisplayMember = "name";
    }

    private void menu_SelectedIndexChanged(object sender, EventArgs e)
    {


    }

    private void label1_Click(object sender, EventArgs e)
    {

    }
}
user3042332
  • 81
  • 1
  • 7
  • 2
    Why don't you make it a private class level field just like you would have in java? – Scott Chamberlain May 11 '17 at 20:15
  • No such thing as a global variable in C#. The best you can get is a [static member of a public class](http://stackoverflow.com/questions/14368129/c-sharp-global-variables). – Guillaume CR May 11 '17 at 20:19
  • @user3042332 Have you tried to debug menu_SelectedIndexChanged and look what is it getting as sender and e? sender is boxed in object it is consider to be your Food object. – Samvel Petrosov May 11 '17 at 20:19

3 Answers3

4

Your List<Food>() is used as the datasource of the control named menu.
So you can always retrieve the same list accessing the datasource as a List<Food>

private void menu_SelectedIndexChanged(object sender, EventArgs e)
{
    List<Food> source = menu.DataSource as List<Food>;
    source.foo();
}

No need to have 'global variables' around.

A more in depth explanation: When you write var foodSource = new List<Food>(); you are allocating a memory area in the heap to keep your list data. The reference to this area is assigned to a local variable named foodSource and finally the DataSource property of the control is assigned to the same reference. However, being the foodSource a local variable created on the stack it is discarded when the code exits from the initFood method, but the memory area on the heap is still in use because the DataSource property keeps a reference to the area. When your code reaches the SelectedIndexChanged event handler you just need to use the as keyword to have a valid reference to the same memory area where the original List<Food> was stored.

Steve
  • 213,761
  • 22
  • 232
  • 286
1

Your best bet would be to have foodSource be a member of the Form1 class:

public partial class Form1 : Form
{
    private List<Food> foodSource;
    public Form1()
    {
        InitializeComponent();
        initFood();
    }

    private void initFood()
    {
        this.foodSource = new List<Food>();

        this.foodSource.Add(new Food { name = "Pizza", price = 10 });
        this.foodSource.Add(new Food { name = "Burger", price = 15 });
        this.foodSource.Add(new Food { name = "Chips", price = 5 });
        this.menu.DataSource = foodSource;
        menu.DisplayMember = "name";
    }
}

Then you can access it from your method.

    private void menu_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.foodSource.foo();
    }
stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
0

You can cast a sender object which should be exactly that guy who holds your data source. But from your example it is not clear WHY do you need to access that list? Doesn't EventArgs e ship all the necessary data?

Zazaeil
  • 3,900
  • 2
  • 14
  • 31