Our assignment is to create a BankAccount class and a Windows Form Application GUI that can be used to track deposits and withdrawals.

The user enters an amount to deposit into a textbox and clicks calculate. The amount is parsed and added to the account balance.

Unfortunately, the account balance is being reset to zero after every entry. So, if I enter "50" and then click calculate, it says the balance is 50. If I then enter "60" and click calculate, it says the balance is 60 instead of 110.

using System;

    public class BankAccount
    {
        private double inValue;

        //I know the problem is here. I just can't figure out how to fix it.
        private double accountBalance;

        public BankAccount()
        {
        }

        public BankAccount(double input)
        {
            inValue = input;
            Deposit();            
        }        

        public double Invalue
        {
            set
            {
                inValue = value;
            }
            get
            {
                return inValue;
            }
        }

        public double AccountBalance
        {
            get
            {
                return accountBalance;
            }
        }

        public void Deposit()
        {
            double accumulator = accountBalance + inValue;
            accountBalance = accumulator;
        }
    }



namespace Assign11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_deposit_Click(object sender, EventArgs e)
        {            
            double input;
            input = double.Parse((putin.Text));

            BankAccount anAccount = new BankAccount(input);

            output.Text = anAccount.AccountBalance.ToString();            
        }

    }
}

April,

Look at your button handler. You are creating anAccount each time. Move the declaration of anAccount to the class level.

Also, rethink your logic. Would it not be better to call Deposit with an amount to deposit?

For your Invalue property on line 20(C# 3.0), you could also opt for automatic properties

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.