Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# Custom Event

Posted on 2012-09-16
3
Medium Priority
?
838 Views
Last Modified: 2012-09-23
Hi,

I have a Security class than contains two variable of the type List<Prices>, called CurrentPrices and NextPrices. The Prices class code is shown below.

The List will contain normally a 1000 plus elements. The seucrity class use's the CurrentPrices type in some calcualtions & after which each one an element is removed from the List. What I would like to happen is when the List count falls below a certain number, say 20 for example an event fires to tell the security object that it needs to copy NexPrices to the CurrentPrices list.

Have little knowledge of events. If this is a poor way of using an event please let me know, however I still like to know how to do this.

Thanks for any help,
M

        public class Price
        {
            public DateTime dtDTime;
            public double BidOpen, BidHigh, BidLow, BidClose;
            public double AskOpen, AskHigh, AskLow, AskClose;
            public double MidOpen, MidHigh, MidLow, MidClose;
        }

Open in new window

0
Comment
Question by:mcs26
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
Naman Goel earned 2000 total points
ID: 38403394
Here is one code for this:

    class Security
    {
        public List<Price> CurrentPrices = new List<Price>();
        public List<Price> NextPrices = new List<Price>();

        public delegate void ItemRemoved();
        public event ItemRemoved ItemRemovedEvent;

        public double Calculate(int index)
        {
            double result = 0.0;
            if (index < CurrentPrices.Count)
            {
                result = CurrentPrices[index].AskOpen + CurrentPrices[index].BidOpen + CurrentPrices[index].MidOpen;
                if (CurrentPrices.Count > 0)
                {
                    CurrentPrices.RemoveAt(index);
                }
            }

            if (CurrentPrices.Count < 3 && ItemRemovedEvent!=null)
            {
                ItemRemovedEvent.Invoke();
            }
            return result;
        }
    }

    public class Price
    {
        public DateTime dtDTime;
        public double BidOpen, BidHigh, BidLow, BidClose;
        public double AskOpen, AskHigh, AskLow, AskClose;
        public double MidOpen, MidHigh, MidLow, MidClose;
    }

Open in new window

0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 38403432
event will be fired as soon as CurrentPrices.Count will be less than 3 by Calculate method.
0
 

Author Closing Comment

by:mcs26
ID: 38426295
Cheers for the code
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question