c# - 由于其在 Windows 窗体中的保护级别而无法访问

标签 c# .net access-modifiers

这是我遇到的第一个问题。说实话,我不知道这是为什么。我读了一点,并将我所看到的大部分重要内容公开(如果不是全部)。所以我想也许这里有人可以解释一下。另外,我试图做到当有人在提款文本框中输入并单击它时,aMtBox 将显示这样的金额。这可行吗?或者我在这里做错了什么

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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
    BankAccount a = new BankAccount();

    public Form1()
    {
        InitializeComponent();
        decimal iBa = 300.00m;
        this.aMtBox.Text = iBa.ToString();
    }
    public void withdrawl_Click(object sender, EventArgs e)
    {
        MessageBox.Show("The balace is... {0:c2}", a.balance.ToString());
    }

    public class BankAccount
    {
        decimal balance;
        decimal iBa;
        decimal num1;

        public decimal Balance
        {
            get { return balance;}
        }
        public decimal IBa
        {
            get { return iBa;}
        }
        public decimal Num1
        {
            get { return num1;}
        }

        public BankAccount()
        {
            iBa = 300.00m;
            num1 = 0.00m;
            balance = iBa - num1;
        }
    }
}
}

最佳答案

改变

a.balance.ToString()

a.Balance.ToString()

a.balance 由于是私有(private)的,外部类无法访问。

关于c# - 由于其在 Windows 窗体中的保护级别而无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19192822/

相关文章:

c# - Microsoft 命名指南 - 什么是 "private interface members"?

c# - 使用 + 的字符串连接是否针对 .NET 中的 StringBuilder 实现进行了优化?

.net - 自由开发者的软件许可

java - 无法使用 Java 语言访问类,但可以使用 Kotlin 访问类

delphi - 将私有(private)/ protected /公共(public)方法设置为事件处理程序是否安全?

.net - 为什么.net 4.5 中的多核 JIT 不是 "on by default"?

c# - ICommand CanExecute 被调用,即使在 ViewModel 从 UI 中移除之后

c# - 使用 AspNet Core OData 和 Entity Framework Core 与 GROUP BY 进行聚合

c# 多条件排序

c# - WCF 中的 WSDL 和 Mex 端点有什么区别