<div>
<asp:Label ID="lbl" runat="server" Text="Balance
Remaining: "></asp:Label>
<asp:Label ID="lblBalance" runat="server"></asp:Label>
<br /><br /><br />
<asp:Label ID="lblmsg" runat="server"></asp:Label>
<br /><br /><br />
<asp:Label ID="lbl1" runat="server" Text="Enter Amount to
Debit"></asp:Label>
<asp:TextBox ID="txtDebit" runat="server"></asp:TextBox>
<br /><br />
<asp:Label ID="lbl2" runat="server" Text="Enter Amount to
Credit"></asp:Label>
<asp:TextBox ID="txtCredit" runat="server"></asp:TextBox>
<br /><br /><br />
<asp:Button ID="btnDebit" runat="server" Text="Debit" OnClick="btnDebit_Click" />
<asp:Button ID="btnCredit" runat="server" Text="Credit" OnClick="btnCredit_Click" />
</div>
</form>
public partial class EventEg : System.Web.UI.Page
{
BankAccount BA = new BankAccount(); // publisher
AccountSubscriber sb =
new AccountSubscriber();
protected void Page_Load(object sender, EventArgs e)
{
lblBalance.Text = BA.Balance.ToString()
}
protected void btnDebit_Click(object sender, EventArgs e)
{
//
MyEventArgs myargs = new MyEventArgs();
//
BA.AccountTransaction += new
AccountTransactionEventHandler(sb.AccountTransactionMessageMethod);
// BA.AccountTransaction += BA. .AccountTransactionMessageMethod(myargs);
// BA.AccountTransaction += sb.AccountTransactionMessageMethod(this,myargs);
BA.AccountTransaction += sb.OnAccountTransactionCompleted;
BA.Debit(Convert.ToDecimal(txtDebit.Text));
BA.SetLabelTextBalance(this, lblBalance);
}
protected void btnCredit_Click(object sender, EventArgs e)
{
//MyEventArgs
myargs = new MyEventArgs();
//BA.Ev_AccountTransaction
+=
sb.AccountTransactionMessageMethod(this, myargs);
BA.AccountTransaction += sb.OnAccountTransactionCompleted;
BA.Credit(Convert.ToDecimal(txtCredit.Text));
BA.SetLabelTextBalance(this, lblBalance);
}
public class AccountSubscriber
{
public void
OnAccountTransactionCompleted(object sender,MyEventArgs args)
{
HttpContext.Current.Response.Write(args.Message);
HttpContext.Current.Response.Write(args.MethodRaisingError);
// EventEg eg = new EventEg();
// Label lb = eg.GetControlFromPageClass() as
Label;
// lb.Text = args.RemainingBalance.ToString();
}
}
} // end of class
BankAccount.cs
namespace DelegatesWebSite
{
// create a delegate
public delegate void AccountTransactionEventHandler(object sender, MyEventArgs args);
public class BankAccount // publisher class
{
private static decimal _balance { get; set; } = 0;
public decimal Balance
{
get
{
return _balance;
}
private set {
_balance = value;
}
}
//
create an Event of delegate type
public event AccountTransactionEventHandler
AccountTransaction;
//
public event EventHandler<MyEventArgs> Ev_AccountTransaction;
protected virtual void OnAccountTransaction(object sender, MyEventArgs a) // this signature
should be same as that of the delegate.
{
// INVOKE THE EVENT WHEN SOMETHING IS DONE IN
THE ACCOUNT
//if (Ev_AccountTransaction != null)
//
Ev_AccountTransaction(this, a); // or like below
AccountTransaction?.Invoke(this, a);
}
public void Credit(decimal amount)
{
Balance += amount;
MyEventArgs s = new MyEventArgs();
s.Message = "Credited
Successfully!!";
s.MethodRaisingError = "Credit
Method";
s.RemainingBalance = Balance;
OnAccountTransaction(this, s);
}
public void Debit(decimal amount)
{
if (_balance >= amount)
{
_balance -= amount;
MyEventArgs s = new MyEventArgs();
s.Message = "Debited
Successfully!!";
s.MethodRaisingError = "Debit
Method";
s.RemainingBalance = Balance;
OnAccountTransaction(this, s);
}
else
{
MyEventArgs s = new MyEventArgs();
s.Message = "Insufficient
Funds!!!!";
s.MethodRaisingError = "Debit
Method";
s.RemainingBalance = Balance;
OnAccountTransaction(this, s);
}
}
public void SetLabelTextBalance(Page page,Label ctl) {
ctl = ctl as Label;
ctl.Text = Balance.ToString();
}
}
public class MyEventArgs : EventArgs
{
public string Message { get; set; }
public string MethodRaisingError { get; set; }
public decimal RemainingBalance { get; set; }
BankAccount ba = new BankAccount();
public MyEventArgs()
{
}
public MyEventArgs(string msg)//: base()
{
this.Message = msg;
this.RemainingBalance = ba.Balance;
}
}
}
0 comments:
Post a Comment