Reputation: 3153
I am using the VS2010 C# language.
I have a form with ENTER button which create an object of class ORDER in Enter_Click(..) event. Now I have another button ADD ITEM which suppose to add a new item in an order when it is clicked.
I tried to access an Order object created in ENTER button click event, in ADDITEM_Click(..) event, I got the following error:
"Object does not exist in current context"
Any help will be greatly appreciated.
Upvotes: 2
Views: 388
Reputation: 13821
The scope of your Order is limited to the Enter_Click()
event because that is where you declare it. Add the line Order myOrder
at the class level and it will work because the object will continue to exist after the Enter_Click()
method finishes.
Upvotes: 2
Reputation: 30127
Declare the Order
object in scope of Class
which contain Enter Button Event Handler
and Add Button Event Handler
For example
partial class MyFormClass
{
Order myOrder;
EnterButton_Click(....)
{
myOrder = new Order();
}
AddButton_Click(....)
{
myOrder.Add(....);
}
}
Upvotes: 3