C#でネストされたクラスについて勉強しようとしています。多くのドキュメントを読んでゴーグリングした後でも、ネストされたクラスをいつ使用するかについてはまだ明確ではありません。しかし、私が理解している限り、小さなサンプルプログラムを作成しました。以下にコードを貼り付けます。このネストされたクラス プログラムは正しいロジックで実装されていますか? . for を使用して実際に入れ子になったクラスは何ですか ?. また、このプログラムで疑問が生じ、プログラムでその疑問を特定しました。私を助けてください ...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Bank bankObj = new Bank();
bankObj.CreateAccount();
bankObj.ShowMyAccountNumber();
}
}
class Bank
{
static int accountNumber; // here if I just declare this as int accountNumber without static it showing an error in the CreatePersonalAccount(int accNo) method's first line ie accountNumber = accNo; as "Cannot access a non-static member of outer type." What actually this error mean ?
public class BankAccountSection
{
public bool CreatePersonalAccount(int accNo)
{
accountNumber = accNo;
return true;
}
}
public void CreateAccount()
{
bool result = new BankAccountSection().CreatePersonalAccount(10001);
}
public void ShowMyAccountNumber()
{
MessageBox.Show(accountNumber.ToString());
}
}