Click here to Skip to main content
15,891,253 members
Home / Discussions / C#
   

C#

 
AnswerRe: Salary calculation Pin
Abhinav S6-Aug-10 1:22
Abhinav S6-Aug-10 1:22 
AnswerRe: Salary calculation Pin
PIEBALDconsult6-Aug-10 3:10
mvePIEBALDconsult6-Aug-10 3:10 
GeneralRe: Salary calculation Pin
JHizzle6-Aug-10 4:34
JHizzle6-Aug-10 4:34 
GeneralRe: Salary calculation Pin
PIEBALDconsult6-Aug-10 14:31
mvePIEBALDconsult6-Aug-10 14:31 
QuestionStatusBar Color Change ???? Pin
Krishna Varadharajan5-Aug-10 20:48
Krishna Varadharajan5-Aug-10 20:48 
AnswerRe: StatusBar Color Change ???? Pin
Rajesh Anuhya5-Aug-10 21:03
professionalRajesh Anuhya5-Aug-10 21:03 
QuestionIs there any technique available for copying one object of class A to another object of class B ? Pin
Nadia Monalisa5-Aug-10 14:26
Nadia Monalisa5-Aug-10 14:26 
AnswerRe: Is there any technique available for copying one object of class A to another object of class B ? [modified] Pin
nlarson115-Aug-10 14:57
nlarson115-Aug-10 14:57 
GeneralRe: Is there any technique available for copying one object of class A to another object of class B ? Pin
Nadia Monalisa5-Aug-10 15:14
Nadia Monalisa5-Aug-10 15:14 
GeneralRe: Is there any technique available for copying one object of class A to another object of class B ? Pin
nlarson115-Aug-10 16:18
nlarson115-Aug-10 16:18 
GeneralRe: Is there any technique available for copying one object of class A to another object of class B ? Pin
Nadia Monalisa5-Aug-10 16:22
Nadia Monalisa5-Aug-10 16:22 
GeneralRe: Is there any technique available for copying one object of class A to another object of class B ? Pin
nlarson115-Aug-10 16:50
nlarson115-Aug-10 16:50 
GeneralRe: Is there any technique available for copying one object of class A to another object of class B ? Pin
Nadia Monalisa5-Aug-10 16:58
Nadia Monalisa5-Aug-10 16:58 
AnswerRe: Is there any technique available for copying one object of class A to another object of class B ? Pin
Giorgi Dalakishvili5-Aug-10 20:27
mentorGiorgi Dalakishvili5-Aug-10 20:27 
AnswerRe: Is there any technique available for copying one object of class A to another object of class B ? PinPopular
Bernhard Hiller5-Aug-10 20:33
Bernhard Hiller5-Aug-10 20:33 
GeneralRe: Is there any technique available for copying one object of class A to another object of class B ? [modified] Pin
Nadia Monalisa5-Aug-10 20:41
Nadia Monalisa5-Aug-10 20:41 
GeneralRe: Is there any technique available for copying one object of class A to another object of class B ? Pin
PIEBALDconsult7-Aug-10 4:21
mvePIEBALDconsult7-Aug-10 4:21 
GeneralRe: Is there any technique available for copying one object of class A to another object of class B ? Pin
il_manti3-Nov-10 23:36
il_manti3-Nov-10 23:36 
AnswerRe: Is there any technique available for copying one object of class A to another object of class B ? Pin
Łukasz Nowakowski5-Aug-10 21:41
Łukasz Nowakowski5-Aug-10 21:41 
QuestionBest Way to Go About Updating Forms When A Piece of Data Is Changed Pin
Matt U.5-Aug-10 11:44
Matt U.5-Aug-10 11:44 
AnswerRe: Best Way to Go About Updating Forms When A Piece of Data Is Changed Pin
Chris Trelawny-Ross9-Aug-10 8:23
Chris Trelawny-Ross9-Aug-10 8:23 
QuestionInterface Events Pin
Ian Grech5-Aug-10 10:52
Ian Grech5-Aug-10 10:52 
Hello all;

I have some classes based on an interface that I need to raise events in an implementing form. However I do not want to create add an event handler definition for all the instances as in Form1_NotWhatIWant in the source sample below.

I would like to define an event handler ONCE, linked to the interface and then, any instantiated class that implements the interface will have the event handler ready.

If not an interface, can it be done through an Abstract baseclass.

I hope the query and code sample are clear enough. A clear answer is appreciated as some I found online did not explain it well. Regardsless, all help is accepted and appreciated.

Thanks
B.

public delegate void CountReachedDelegate(IBase iBase, string Message);


public interface IBase
{
  Int32 Trigger {get;}
  String Name { get; }

  void Count(int Limit);

  event CountReachedDelegate CountReached;
}

public class SubIntA: IBase
{
  #region IBase Members

  private int _T = 20;
  public int Trigger
  {
    get { return _T; }
  }

  string _N = "A";
  public string Name
  {
    get { return _N; }
  }

  public void Count(int Limit)
  {
    for (int x = 0; x < Limit; x++)
      if (x % _T == 0)
        if (CountReached != null)
          CountReached(this, _N + "=" + x.ToString());
  }

  public event CountReachedDelegate CountReached;

  #endregion
}

/*Like SubIntA I have SubIntB, SubIntC, etc.....*/


/*This works but it is not what I want to do*/
public partial class Form1_NotWhatIWant : Form
{
  IBase A = new SubIntA();
  IBase B = new SubIntB();

  public Form1_NotWhatIWant ()
  {
    InitializeComponent();

    A.CountReached += new CountReachedDelegate(CountReached);
    B.CountReached += new CountReachedDelegate(CountReached);
  }

  void CountReached(IBase iBase, string Message)
  {
    /*show message from event in a listbox*/
    listBox1.Items.Insert(0, iBase.GetType().ToString() + ": " + Message);
  }

  private void button1_Click(object sender, EventArgs e)
  {
    A.Count(100);
  }

  private void button2_Click(object sender, EventArgs e)
  {
    B.Count(150);
  }
}




public partial class Form1_WhatIWouldLike : Form
{
  IBase A = new SubIntA();
  IBase B = new SubIntB();

  public Form1_WhatIWouldLike()
  {
    InitializeComponent();

    IBase.CountReached += new CountReachedDelegate(CountReached);
  }

  void CountReached(IBase iBase, string Message)
  {
    /*show message from event in a listbox*/
    listBox1.Items.Insert(0, iBase.GetType().ToString() + ": " + Message);
  }

  private void button1_Click(object sender, EventArgs e)
  {
    A.Count(100);
  }

  private void button2_Click(object sender, EventArgs e)
  {
    B.Count(150);
  }
}

AnswerRe: Interface Events Pin
PIEBALDconsult5-Aug-10 16:56
mvePIEBALDconsult5-Aug-10 16:56 
AnswerRe: Interface Events Pin
Chris Trelawny-Ross9-Aug-10 8:53
Chris Trelawny-Ross9-Aug-10 8:53 
GeneralRe: Interface Events Pin
Ian Grech13-Aug-10 0:34
Ian Grech13-Aug-10 0:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.