Darren Heffernan
Darren Heffernan

Reputation: 53

Property or indexer must have at least one accessor

I'm learning C#, trying to get to grips with accessors at the moment.
I'm going nuts looking at this, I have no idea what I've done wrong:

class BankAccount
{
    // *PROPERTIES* 
    private int _initialDeposit = 0;

    // **ACCESSORS** 
    public int SavingsAccount
    {
        set
        {
            _initialDeposit = value;
        }
        get
        {
            return _initialDeposit;
        }
    }
}

The Form looks like this:

public partial class BankForm : Form
{
    private BankAccount _myAccount;

    public BankForm()
    {
        InitializeComponent();
        _myAccount = new BankAccount();
    }

    private void initialDepositButton_Click(object sender, EventArgs e)
    {
        _myAccount.SavingsAccount = Convert.ToInt32(initialDepositTextBox.Text);
        bankAccountListBox.Text = "Account opened with initial Deposit " + initialDepositTextBox.Text;
    }
}

But I get this error:

Property or indexer must have at least one accessor

Upvotes: 5

Views: 26729

Answers (1)

jdweng
jdweng

Reputation: 34429

I'm not getting any errors. Move location of private BankAccount _myAccount;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BankForm
{

    public partial class BankForm : Form
    {
        public BankForm()
        {
            InitializeComponent();
            _myAccount = new BankAccount();
        }
        private BankAccount _myAccount;

        private void initialDepositButton_Click(object sender, EventArgs e)
        {
            _myAccount.SavingsAccount = Convert.ToInt32(initialDepositTextBox.Text);
            bankAccountListBox.Text = "Account opened with initial Deposit " + initialDepositTextBox.Text;
        }

    }
    class BankAccount
    {
        // *PROPERTIES* 
        private int _initialDeposit = 0;

        // **ACCESSORS** 
        public int SavingsAccount
        {
            set
            {
                _initialDeposit = value;
            }
            get
            {
                return _initialDeposit;
            }
        }
    }

}
​

Upvotes: 3

Related Questions