Advertisement

10.03.2006 at 01:45PM PDT, ID: 22011724
[x]
Attachment Details

Invoke a method when object added to generic collection?

Asked by sapbucket in Miscellaneous Programming

Hello,

 I have a generic collection class that looks something like this:

public class SignalEventList : List<SignalEvent>
{}

Whenever I add a new SignalEvent to a SignalEventList I want to perform a calculation:

public class SignalEventList : List<SignalEvent>
{
   List<double> results = new List<double>;

    private void doCalculation()
    {
         foreach(SignalEvent se in this) { results.Add(doCalculation());}
    }
}


except, I am really sick of calling "doCalculation()" from client code. I get two lines: 1.) the actual Add(), 2.) the annoying call to doCalculation(). I want the client to be completely ignorant of the call to doCalculation(). So, what I want is the client to Add(SignalEvent) and then doCalculation() occurs behind the scenes.

In perl, I might do this with a Tie variable. Any ideas for C#?

Many thanks, please let me know if you need more clarification.Start Free Trial
[+][-]10.03.2006 at 02:00PM PDT, ID: 17654697

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Miscellaneous Programming
Sign Up Now!
Solution Provided By: rmahesh
Participating Experts: 2
Solution Grade: A
 
 
[+][-]10.03.2006 at 02:13PM PDT, ID: 17654804

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.05.2006 at 02:37PM PDT, ID: 17672535

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.09.2006 at 04:08PM PDT, ID: 17694552

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.16.2006 at 07:35AM PDT, ID: 17739170

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32