属性或索引器必须至少有一个访问者

时间:2015-06-08 12:30:32

标签: c#

我正在学习C#,目前试图掌握配件 我很疯狂地看着这个,我不知道我做错了什么:

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

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

表格如下:

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

但是我收到了这个错误:

  

属性或索引器必须至少有一个访问者

1 个答案:

答案 0 :(得分:1)

我没有收到任何错误。移动私人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;
            }
        }
    }

}
​