Delegates and Events Problem
-
I have created a class called BankAccount. the samle is a floows public delegate void myEventDelegate(); class BankAccount { public static long Balance; public event myEventDelegate myEvent; public void WithDraw(long amount) { Balance -= amount; myEvent(); } } i then wired my event in the frm load to an event handler "OnWithdraw" i also a textbox where by i subtract the money to be withdrawn from the account. The method display an event to the user when the account bal < 0; when i run the program i receiven a NullArgumentException error on the "myEvent" int BankAccount class how do i make my event work?
-
I have created a class called BankAccount. the samle is a floows public delegate void myEventDelegate(); class BankAccount { public static long Balance; public event myEventDelegate myEvent; public void WithDraw(long amount) { Balance -= amount; myEvent(); } } i then wired my event in the frm load to an event handler "OnWithdraw" i also a textbox where by i subtract the money to be withdrawn from the account. The method display an event to the user when the account bal < 0; when i run the program i receiven a NullArgumentException error on the "myEvent" int BankAccount class how do i make my event work?
You should never call a delegate without firstchecking if it is null. Obviously your event wiring code is broken. You need to post it for us to comment further. Have you checked in the debugger that it gets wired up ?
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
You should never call a delegate without firstchecking if it is null. Obviously your event wiring code is broken. You need to post it for us to comment further. Have you checked in the debugger that it gets wired up ?
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Sorry for that. this is my first time posting. //From BankAccount.cs using System; using System.Collections.Generic; using System.Text; namespace Delegates_and_Events { public delegate void myEventDelegate(); class BankAccount { public static long Balance; public event myEventDelegate myEvent; public void WithDraw(long amount) { Balance -= amount; myEvent(); } } } //from frmMain.cs private void Form1_Load(object sender, EventArgs e) { BankAccount myAcc = new BankAccount(); BankAccount.Balance = 1000; myAcc.myEvent += new myEventDelegate(OnWithDraw); } public void OnWithDraw() { if (BankAccount.Balance < 0) { MessageBox.Show("Account Overdrawn"); } } I also have a textbox where i am getting the value which i subtract using the following private void btnWithDraw_Click(object sender, EventArgs e) { BankAccount myAcc = new BankAccount(); myAcc.WithDraw(long.Parse(txtAmount.Text)); txtBankBalance.Text = BankAccount.Balance.ToString(); }