End Google Ads 201810 - BS.net 01 --> I am trying to understand delegates and events in C Sharp. I am finding the subject confusing. Please consider the following program:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class StockArgs : EventArgs {
public StockArgs( String symbol, string price, int volume )
{
this.symbol = symbol;
this.price = Decimal.Parse(price);
this.volume = volume;
}
public string Symbol {
get
{ return symbol; }
}
public string Price {
get
{ return Price; }
}

private string symbol;
private decimal price;
private int volume;
}

public class Stock {
public delegate void StockPiraceAlertHandler( object source, StockArgs a);
public event StockPiraceAlertHandler OnAlert;

public delegate void VolumeAlertHandler(object source, StockArgs a);
public event VolumeAlertHandler OnVolumeAlert;

public void Alert()
{
if ( OnAlert != null )
OnAlert( this, new StockArgs("ORCL", "14.50", 100000) );
}

}

public class Main {
public static void Main()
{
Stock stock1 = new Stock();
stock1.OnAlert(null, null);
}
}

In the main routine (called Main) I want to trigger the alert called OnAlert. However, the code does not compile. Furthermore, the compiler error message implies that alerts can be only generated by the class that defines the alert. If that is true, then it seems like a major deficiency with alerts in C sharp. However, I suspect I am missing one or more key points. Please enlighten me.

I thank the group in advance for their responses.

Bob