Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

Event not being raised?

I am not sure why the "BothPaidOff" event is not being raised inside of "CheckDebtStatus(  )" method.

Tried setting conditions for the breakpoint to stop, but those conditions are not happening:

(this._helocAmountOwed < 3000) || (m._mortAmountOwed < 3000)

breakpoint is on this line inside CheckDebtStatus(  ):

 else if ((this._helocAmountOwed <= 0.0) && (m._mortAmountOwed <= 0.0))






=================

//rename this to anything OTHER than "debug" in order to turn-off the reporting to the Output window
#define debug

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

namespace Driven
{

    public class DebugOut
    {
        #region Constructors
        public DebugOut()
        { }
        #endregion

        #region dOut
        public void dOut(string message)
        {
            #if debug
            System.Diagnostics.Debug.WriteLine(message);
            #endif
        }
        #endregion
    }








    public class Heloc
    {
        #region Variables

        public event EventHandler HelocAmountOwedIsZero;
        public event EventHandler HelocAmountOwed;
        public event EventHandler BothPaidOff;

        public int _helocReapplicationIntervalInMonths = 18;
        public double _helocAmountOwed;
        public double _helocAmountAvailable;
        public double _helocInterestRate;
        public double _helocMonthlyBills;        
        private DebugOut dbg = new DebugOut();
        #endregion

        #region Constructors
        public Heloc(double amountOwed, double initialAmountAvailable, double interestRate, double monthlyBills)
        {
            _helocAmountOwed = amountOwed;
            _helocAmountAvailable = initialAmountAvailable;
            _helocInterestRate = interestRate;
            _helocMonthlyBills = monthlyBills;        

            HelocAmountOwedIsZero = null;
            HelocAmountOwed = null;
            BothPaidOff = null;
        }
        #endregion

        #region HowMuchToApplyToMortgage
        public double HowMuchToApplyToMortgage(Mortgage m)
        {
            //Given to Tom Knowlton by Kevin
           
            double amountToApply = 0;
            if (_helocAmountAvailable >= m._mortAmountOwed)
            {
                amountToApply = m._mortAmountOwed;
            }
            else
            {
                amountToApply = this._helocAmountAvailable - (2 * _helocMonthlyBills);
            }

            if (amountToApply < 0)
            {
                amountToApply = 0;
            }

            dbg.dOut("");
            dbg.dOut("                         AVAILABLE:  " + this._helocAmountAvailable.ToString());
            dbg.dOut("SUGGESTED AMOUNT TO USE FROM HELOC:  " + amountToApply.ToString());
            dbg.dOut("                    CURRENTLY OWED:  " + this._helocAmountOwed.ToString());
            dbg.dOut("");
     

            return amountToApply;

        }
        #endregion


        #region CheckDebtStatus
        public void CheckDebtStatus(Mortgage m)
        {
            if (this._helocAmountOwed > 0.0)
            {
                if (HelocAmountOwed != null)
                {
                    HelocAmountOwed(this, null);
                }
            }
            else if(this._helocAmountOwed <= 0.0)
            {
                if (HelocAmountOwedIsZero != null)
                {
                    HelocAmountOwedIsZero(this, null);
                }
            }
            else if ((this._helocAmountOwed <= 0.0) && (m._mortAmountOwed <= 0.0))
            {
                if (BothPaidOff != null)
                {
                    BothPaidOff(this, null);
                }
            }
        }
        #endregion


        #region MakeHelocPayment
        public void MakeHelocPayment(double helocPayment)
        {            
            //If we owe on the Heloc, then let's make a payment on it...
            if (this._helocAmountOwed > 0.0)
            {
                double periodInterestPayment;

                //if (HelocAmountOwed != null)
                //{
                //    //raise the event that the Heloc amount owed is now ZERO
                //    HelocAmountOwed(this, null);
                //}

                //calculate interest payment
                periodInterestPayment = this._helocAmountOwed * (this._helocInterestRate / 12);

                //is the amount owed on the heloc less than the payment amount?
                if ((this._helocAmountOwed + periodInterestPayment) < helocPayment)
                {
                    //If so, then pay-off the Heloc NOW!
                    this._helocAmountOwed = 0.0;
                    dbg.dOut("");
                    dbg.dOut("");
                    dbg.dOut("");
                    dbg.dOut("");
                    dbg.dOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  Heloc was just paid-off!!!");
                    dbg.dOut("");
                    dbg.dOut("");
                    dbg.dOut("");
                    dbg.dOut("");
                }
                else  // just make a normal Heloc payment
                {
                    this._helocAmountOwed = (this._helocAmountOwed + periodInterestPayment) - helocPayment;
                    dbg.dOut("Heloc payment of " + helocPayment.ToString() + " was made, Heloc amount owed is:  " + this._helocAmountOwed.ToString());
                }                
            }          
        }
        #endregion
               
        #region ReturnHomeEquity
        public double ReturnHomeEquity(Driven.Mortgage m)
        {
            return m._mortOriginalAmountOwed - m._mortAmountOwed;
        }
        #endregion



        #region HelocCanBeRenewed
        public bool HelocCanBeRenewed(int numberOfMonthsAccrued, Driven.Mortgage m)
        {
            //Update available Heloc credit line every 18 months...
            if ((numberOfMonthsAccrued % this._helocReapplicationIntervalInMonths) == 0)
            {
                _helocAmountAvailable = ReturnHomeEquity(m);
                dbg.dOut("");
                dbg.dOut("");
                dbg.dOut("");
                dbg.dOut("                                  THE HELOC CAN RENEW EVERY 18 MONTHS");
                dbg.dOut("");
                dbg.dOut("");
                dbg.dOut("");

                return true;
            }
            else
            {
                return false;
            }


        }
        #endregion
    }
   






    public class Mortgage
    {
        #region Variables

        public event EventHandler MortAmountOwedIsZero;
        public event EventHandler MortAmountOwed;
        public event EventHandler BothPaidOff;
               
        public double _mortOriginalAmountOwed;
        public double _mortAmountOwed;        
        public double _mortInterestRate;
        public double _interestAccrued;
        private DebugOut dbg = new DebugOut();
        #endregion
               
        #region Constructors
               

        public Mortgage(double amountOwed, double interestRate)
        {
            //initialize mortgage object...
            _mortOriginalAmountOwed = amountOwed;
            _mortAmountOwed = amountOwed;            
            _mortInterestRate = interestRate;
           
            //set events to null...
            MortAmountOwedIsZero = null;
            MortAmountOwed = null;
            BothPaidOff = null;
        }
        #endregion

        #region CheckDebtStatus
        public void CheckDebtStatus(Heloc h)
        {
            if (this._mortAmountOwed > 0.0)
            {
                if (MortAmountOwed != null)
                {
                    MortAmountOwed(this, null);
                }
            }
            else if (this._mortAmountOwed <= 0.0)
            {
                if (MortAmountOwedIsZero != null)
                {
                    MortAmountOwedIsZero(this, null);
                }
            }
             else if ((h._helocAmountOwed <= 0.0) && (this._mortAmountOwed <= 0.0))
            {
                if (BothPaidOff != null)
                {
                    BothPaidOff(this, null);
                }
            }
        }
        #endregion

        #region CalcNormalTermNoHelocApplied
        public void CalcNormalTermNoHelocApplied(double paymentAmount)
        {
            while (this._mortAmountOwed > 0.0)
            {
                this.MakeMortgagePayment(paymentAmount);
            }
        }
        #endregion


        #region MakeMortgagePayment
        public void MakeMortgagePayment(double mortPayment)
        {
            double periodInterestPayment = 0;
            //calculate the monthly interest
            periodInterestPayment = this._mortAmountOwed * (this._mortInterestRate / 12);
           
            //to calculate interest saved
            _interestAccrued += periodInterestPayment;
           
            //Only make a payment if there is still money owed...
            if (this._mortAmountOwed > 0.0)
            {                
                //If the amount owed is less than or equal to the payment amount, then pay-off the loan
                if ((this._mortAmountOwed + periodInterestPayment) <= mortPayment)
                {
                    this._mortAmountOwed = 0.0;
                    dbg.dOut("");
                    dbg.dOut("");
                    dbg.dOut("");
                    dbg.dOut("");
                    dbg.dOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  Mortgage has been paid-off completely!!!!!!!");
                    dbg.dOut("");
                    dbg.dOut("");
                    dbg.dOut("");
                    dbg.dOut("");
                }
                else  //otherwise just make a normal payment
                {

                    //For debug / reporting purposes
       
                    dbg.dOut("");
                    dbg.dOut("Previous mortgage balance owed (" + this._mortAmountOwed.ToString() + " + " + periodInterestPayment.ToString() + "):  " + (this._mortAmountOwed + periodInterestPayment));
                    dbg.dOut("Mortgage payment made:  " + mortPayment.ToString());

                    //add monthly interest to entire amount owing, then pay on THAT
                    this._mortAmountOwed = (this._mortAmountOwed + periodInterestPayment) - mortPayment;

                    dbg.dOut("After payment, now mortgage balance is now:  " + this._mortAmountOwed.ToString());
                }
            }      

        }
        #endregion

    }// end of Mortgage class  









    public class DrivenExpert
    {
        #region Variables        
        public Heloc _heloc;
        public Mortgage _mortgage;
        public Mortgage _mortgageNoHeloc;
        public int _mortMonthsAccrued = 0;
        public int _helocMonthsAccrued = 0;

        public double _monthlyMortPay;
        public double _monthlyHelocPay;

        private DebugOut dbg = new DebugOut();        
        #endregion

        #region Constructors
        public DrivenExpert(double mortAmountOwed,
            double mortInterestRate,
            double monthlyMortPay,
            double helocAmountOwed,
            double helocAmountAvailable,
            double helocInterestRate,
            double helocMonthlyBills,
            double helocMonthlyPayment)
        {
            _monthlyMortPay = monthlyMortPay;
            _monthlyHelocPay = helocMonthlyPayment;

            _mortgage = new Mortgage(mortAmountOwed, mortInterestRate);
            _mortgageNoHeloc = new Mortgage(mortAmountOwed, mortInterestRate);
            _heloc = new Heloc(helocAmountOwed, helocAmountAvailable, helocInterestRate, helocMonthlyBills);

            _heloc.HelocAmountOwed += new EventHandler(this.FinancedItemOwing);
            _mortgage.MortAmountOwed += new EventHandler(this.FinancedItemOwing);
            _heloc.HelocAmountOwedIsZero += new EventHandler(this.FinancedItemIsZero);
            _mortgage.MortAmountOwedIsZero += new EventHandler(this.FinancedItemIsZero);
            _heloc.BothPaidOff += new EventHandler(this.BothPaidOff);
            _mortgage.BothPaidOff += new EventHandler(this.BothPaidOff);

        }
        #endregion      
               

        public void BothPaidOff(object sender, EventArgs e)
        {
            dbg.dOut(sender.ToString() + " says DONE, amount owing is zero on both accounts");
        }

        #region GetOutOfDebt
        public void GetOutOfDebt()
        {
            _mortgage.CheckDebtStatus(_heloc);
            _heloc.CheckDebtStatus(_mortgage);

            int mma = this._mortMonthsAccrued;
            int hma = this._helocMonthsAccrued;
            dbg.dOut("Months to pay-off Mort:  " + mma.ToString());
            dbg.dOut("Months to pay-off Heloc:  " + hma.ToString());
            dbg.dOut("Months to pay-off Both:  " + (Math.Min(mma, hma) + (Math.Max(mma, hma) - Math.Min(mma, hma))));
        }

        public double CalcInterestSaved()
        {
            _mortgageNoHeloc.CalcNormalTermNoHelocApplied(_monthlyMortPay);

            return _mortgageNoHeloc._interestAccrued - _mortgage._interestAccrued;
        }

        #endregion

        #region FinancedItemOwing
        public void FinancedItemOwing(object sender, EventArgs e)
        {
            if (sender is Driven.Mortgage)
            {
                if (_heloc._helocAmountOwed == 0.0)
                {
                    double tempHowMuchToApply = this._heloc.HowMuchToApplyToMortgage(_mortgage);
                    this._heloc._helocAmountOwed = tempHowMuchToApply;
                    _mortgage.MakeMortgagePayment(this._monthlyMortPay + tempHowMuchToApply);                    
                }
                else
                {
                    _mortgage.MakeMortgagePayment(this._monthlyMortPay);                
                }
                _mortMonthsAccrued++;
                dbg.dOut("Mort Month:  " + this._mortMonthsAccrued.ToString());
                _heloc.CheckDebtStatus(_mortgage);                
            }
            else if (sender is Driven.Heloc)
            {
                if (_mortgage._mortAmountOwed == 0.0)
                {
                    _heloc.MakeHelocPayment(this._monthlyHelocPay + this._monthlyMortPay);                    
                }
                else
                {                    
                    _heloc.MakeHelocPayment(this._monthlyHelocPay);                    
                }
                _helocMonthsAccrued++;
                _heloc.HelocCanBeRenewed(_helocMonthsAccrued, _mortgage);                    
                dbg.dOut("Heloc Month:  " + this._helocMonthsAccrued.ToString());
                _mortgage.CheckDebtStatus(_heloc);
            }            
        }
        #endregion
               

        //Event that gets called when either Heloc or Mort is zero
        #region FinancedItemIsZero
        public void FinancedItemIsZero(object sender, EventArgs e)
        {
            dbg.dOut("Financed Item is Zero.  Sender is:  " + sender.ToString());

            //if sender is Mortgage then Mortgage owing is $0
            //if sender is Heloc then Heloc owing is $0
           
            if (sender is Driven.Mortgage)
            {
                dbg.dOut("Mortgage payment should be applied to Heloc, since Mortgage owing is now:  " + _mortgage._mortAmountOwed.ToString());
               
                _heloc.CheckDebtStatus(_mortgage);            
            }

            if (sender is Driven.Heloc)
            {
                //Logic question...
                //why is this not needed...why is it empty?????????
                //_mortgage.CheckDebtStatus(_heloc);
            }

        }
        #endregion        

    }//end of DrivenExpert class

}//end of namespace driven
0
Tom Knowlton
Asked:
Tom Knowlton
  • 3
  • 2
2 Solutions
 
Bob LearnedCommented:
Are you reaching this line?

    if (BothPaidOff != null)

Bob
0
 
REA_ANDREWCommented:
where is your delegate signature for the EventHandlers, as I do not see any nor do I see any generic methods of the EventHandler either. i.e.

public event EventHandler<YourEventArgs> YourEvent;

That right?

Andrew
0
 
Bob LearnedCommented:
Andrew,

EventHandler is from the System namespace:

[Serializable, ComVisible(true)]
public delegate void EventHandler(object sender, EventArgs e);

Bob
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Tom KnowltonWeb developerAuthor Commented:
I found the answer.  The problem was with the structure and order of my if...else  inside of CheckDebtStatus:

Here is the corrected version of it  (Mortgage and Heloc classes had the same issue).  This formation allows the short-circuiting to work as intended:

public void CheckDebtStatus(Heloc h)
        {
            if ((h._helocAmountOwed <= 0.0) && (this._mortAmountOwed <= 0.0))
            {
                if (BothPaidOff != null)
                {
                    BothPaidOff(this, null);
                }
            }
            else if (this._mortAmountOwed > 0.0)
            {
                if (MortAmountOwed != null)
                {
                    MortAmountOwed(this, null);
                }
            }
            else if (this._mortAmountOwed <= 0.0)
            {
                if (MortAmountOwedIsZero != null)
                {
                    MortAmountOwedIsZero(this, null);
                }
            }            
        }
0
 
REA_ANDREWCommented:
"Andrew,

EventHandler is from the System namespace:"

using this

public event EventHandler<YourEventArgs> YourEvent;

is the new generic way of declaring events, it does not require a delegate signature.

Andrew

0
 
REA_ANDREWCommented:
FYI Bob

:-)

Andrew
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now