I don't really know how to explain this but i have tried a few thing and they don't work. Basically i want to have an Action on a item, the action is kinda like the items functionality. here is my code it's not that advanced.
class Item
{
private int itemCount;
private Action itemUsage;
string name;
//Item.Item()
public Item(string _name, Action _itemUsage, int _itemCount)
{
name = _name;
itemUsage = _itemUsage;
itemCount = _itemCount;
//laterInvokes itemUsage
}
//Item.UseItem
public void UseItem()
{
//TYPE:ACTION, is set in constructor
this.itemUsage();
}
}
And.
class Program
{
//Program.Main(string[] args)
public static void Main(string[] args)
{
Item[] items = new Item[11];
//for every item in items;
for (int i = 0; i < items.Length; i++)
{
//How do i save 'a's lambda with out it coming back here?
Action a = () => Console.WriteLine("Used Item " + i);
items[i] = new Item("Item " + i, a, 1);
}
items[0].UseItem();
items[3].UseItem();
items[4].UseItem();
items[7].UseItem();
//Expect result;
//"Used Item 0"
//"Used Item 2"
//"Used Item 3"
//"Used Item 7"
//Actual Result
//"Used Item 5"
//"Used Item 5"
//"Used Item 5"
//"Used Item 5"
Console.ReadLine();
}
}
The problem as i can see it is that the i variable is not saved in the lambda.
Action a = () => Console.WriteLine("Used Item " + i);
so whenever i call item[x].UseItem() it goes back inside the for loop to the Action a declaration and 'uses' the i there which is now 5.