Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Delegates and Events Problem

Delegates and Events Problem

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jowecape
    wrote on last edited by
    #1

    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?

    C 1 Reply Last reply
    0
    • J jowecape

      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?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      J 1 Reply Last reply
      0
      • C Christian Graus

        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 )

        J Offline
        J Offline
        jowecape
        wrote on last edited by
        #3

        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(); }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups