John Acosta
John Acosta

Reputation: 47

Updating fields from form

I'm having a small problem with this program. This program is designed to use polymorphism. I have written one base class and two derived classes.

We are supposed to make a base class(bank Account) array and fill that with three bank account objects. We then assign each bank account object a new object using it's overloaded constructor.

 public partial class Form1 : Form
    {
     //Base class array
     BankAcct[] b = new BankAcct[3];



    public Form1()
    {
        InitializeComponent();

        //This is not getting current values from form!
        int accountNum;
        int atmNum;
        int pinNum;

        an = Convert.ToInt32(accountNumber.Text);
        p = Convert.ToInt32(pin.Text);
        atm = an - p;

        //base class
        b[0] = new BankAcct(name.Text, 500.00M, accountNum);

        //this derived class inherits from bankAcct name, account number, and 
        //the decimal which is the balance assigned to the Account
        //private variables are atm and pin in this class
        b[1]= new SilverBankAcct(name.Text, an, 1500.00M, atmNumber, pinNum);

        //this derived class inherits from SilverBankAcct atm, pin,
        //has one private variable the decimal at the end which is the interest
        b[2] = new GoldBankAcct(name.Text, accountNum, 25000.00M, atm, pinNum, 0.05M);

    }

My problem is that when I instantiate my objects in the Form1 Constructor The fields do not update from the form and the current values in those fields are being ignored. I tried assigning the information by accessing properties in my base class and assigning the values from the form there, but the problem comes when I try to update my atm number and pin number which are private variables that go into my SilverBankAcct class, and GoldBankAcct class.

private void button1_Click(object sender, EventArgs e)
    {


        b[0].fName = name.Text;
        b[1].fName = name.Text;
        b[2].fName = name.Text;
        //do the same for account number which works, but how am I supposed to update atm and pin from the form when I have no access to these variables I only have access to the base class?
     }

What would be a better way to ensure that the values being passed along are being updated to the current values from the form when the button is clicked?

Upvotes: 1

Views: 137

Answers (1)

Daniel
Daniel

Reputation: 2004

You can write something like this:

private void assignPinNumber(BankAcct account, int newPinNumber)
{
   SilverBankAcct silver = account as SilverBankAcct;
   if(silver != null)
      silver.pinNumber = newPinNumber;
}

Upvotes: 1

Related Questions