Link to home
Start Free TrialLog in
Avatar of mcs26
mcs26

asked on

C# Custom List firing an event to update base class

Hi,

I have a base class called OrderBlock which contains three properties, orderSend (int), orderHold (int) & Orders (List<Order>. It also contains a class called Order, structure is shown below in the code block, obviously a very simple snapshot.

The Orders list can contain anywhere from 10 items to 1000 items. What I want to know how to do is to create a event for each item in the list and for the orderSend & orderHold properites in the class OrderBlock to subscribe to each one.

The reason being is I have a WPF application that contains a datagrid which is bound to my list Orders. The user can change the status of an Order from "Hold" to "Send" so I then need to update the orderSend & orderHold properties.

class OrderBlock
{
 int orderSend;
 int orderHold;
 List<Order> Orders;

 class Order
 {
  string OrderStatus;
  // other properties etc
 }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of bearsomg
bearsomg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcs26
mcs26

ASKER

Thanks for the reply bearsomg.

I basically what I want is when the property 'OrderStatus' changes in any of the items an event is raised.

I want orderSend to subcribe to all these events (I.e. each item in the list) and to increment by 1 if the OrderStatus is changed to 'Send' and vice versa for orderHold I.e that would reduced by 1.

Thanks again,
M
In that case, the code I posted above will work for firing an event when OrderStatus changes. When you instantiate each order make sure to assign the event to an event handler function just like you would any other event. In that function simply check what the order status is (the OrderStatusChangedEventArgs parameter will contain the old status and the new status) and act on the orderSend and orderHold variables accordingly.
Avatar of mcs26

ASKER

I don't quiet follow. I can see how every Order in the list will raise an event when it's status is changed. However I don't see for example how orderSend in OrderBlock subscribes to each item's event in the Orders list? I have used events before but think the list is confusing me.

Thanks again for your help!
Firstly, sorry for the late reply, I've been busy with classes the past few days.

Below I included a basic Program class that I wrote to give you an idea of how to use my events method:

class Program
    {
        static OrderBlock orderBlock; //the master orderblock

        static void Main(string[] args)
        {
            orderBlock = new OrderBlock();

            orderBlock.Orders = new List<OrderBlock.Order>();

            //create some arbitrary orders
            for (int i = 0; i < 5; i++)
            {
                OrderBlock.Order newOrder = new OrderBlock.Order(); //instantiate a new Order
                newOrder.OrderStatus = i.ToString(); //stick this in here because why not
                newOrder.OrderStatusChanged += new OrderStatusChangedEventHandler(order_OrderStatusChanged); //register the status changed event
            }
        }

        static void order_OrderStatusChanged(object sender, OrderStatusChangedEventArgs e) //<- will get called every time ANY ORder in the order list is changed
        {
            if (e.GetNewStatus() == "Send") //check to see if it was changed to send
            {
                orderBlock.orderSend++; //update variables
                orderBlock.orderHold--;
            }
        }
    }

Open in new window


Also, I forgot to include that the public modifier should be on all the member variables and Order class inside the OrderBlock class.