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

mcs26Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bearsomgCommented:
I think I am having trouble understanding what you would like to do. Do you want to create events that get triggered when orderSend, orderHold, and OrderStatus change? If so, something like this will work:

First create a separate class for the event itself:

OrderStatusChangedEvent.cs
public delegate void OrderStatusChangedEventHandler(object sender, OrderStatusChangedEventArgs e);
    class OrderStatusChangedEventArgs : EventArgs
    {
        private string oldStatus, newStatus;
        public OrderStatusChangedEventArgs(string oldStatus, string newStatus)
        {
            this.oldStatus = oldStatus;
            this.newStatus = newStatus;
        }
        public string GetOldStatus()
        {
            return oldStatus;
        }
        public string GetNewStatus()
        {
            return newStatus;
        }
    };

Open in new window


Now add the get and set to OrderStatus, and put an instance of OrderStatusChangedEventHandler into the Order class:

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

        class Order
        {
            private string Status;

            public event OrderStatusChangedEventHandler OrderStatusChanged;
            public string OrderStatus
            {
                get
                {
                    return Status;
                }
                set
                {
                    OrderStatusChanged(this, new OrderStatusChangedEventArgs(Status, value));
                    Status = value;
                }
            }
            // other properties etc
        }
    }

Open in new window


The reason I added another variable called Status is because OrderStatus is now technically a "wrapper" - nothing can actually be assigned to it, but it can be treated like a normal variable for assignment. When a value is assigned to OrderStatus, it will set it to the private variable Status, then fire the OrderStatusChanged event which can be used just like any other event would be. When ORderStatus is read, it will simply return the value of Status.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcs26Author Commented:
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
0
bearsomgCommented:
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.
0
mcs26Author Commented:
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!
0
bearsomgCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.