问题描述
我想就我面临的问题向专家咨询。在自下而上的方法中,我有一个类 StandardAccount,用几个简单的属性初始化,其中一个是 enum(和一个在实例化时自动设置的 GUID,如
public class StandardAccount
{
private Guid _id;
private string _accName;
private AccountType _accType;
private double _balance = 0;
public enum AccountType
{
[Description("Asset")]
AT,[Description("Liability")]
LY,[Description("Profit")]
PT,[Description("Loss")]
LS
}
public StandardAccount(string name,AccountType type)
{
this._id = Guid.NewGuid();
this._accName = name;
this._accType = type;
this.Balance = 0;
}
public double Balance { get => _balance; set => _balance = value; }
}
Book 类必须有一个或多个这些 StandardAccounts 的列表,而 Accounting 类必须有许多 Books。我以一种可搜索的方式准备我的 Book 类(将有许多 StandardAccount 列表,我需要能够稍后在这些列表中通过 GUID 找到 StandardAccount)。我按如下方式设置我的 Book 类:
public class Book
{
private string _bookName;
private short _bookNum;
private Guid _bookId;
public List<StandardAccount> Accounts { get; set; }
public IEnumerator<StandardAccount> GetEnumerator() => Accounts.GetEnumerator();
//compile time err: containing type does not implement interface 'IEnumerable'
IEnumerator IEnumerable.GetEnumerator() => Accounts.GetEnumerator();
//updated
public void Add(string name,StandardAccount.AccountType type) => Accounts.Add(new StandardAccount(name,type));
public Book(Guid? id,short BookNumber,string BookName,IEnumerable<StandardAccount> Account)
{
this._bookId = id ?? Guid.NewGuid();
this._bookNum = BookNumber;
this._bookName = BookName;
this.Accounts = new List<StandardAccount>();
}
public Guid id { get => _bookId; set => _bookId = value; }
}
我有这两个错误使我无法继续前进,我不理解它们(因为我试图在“母亲”Book 类中而不是在 StandardAccount 中实现枚举器) 有人可以帮忙和建议吗?
注意:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
我正在尝试这样做:
//this works fine
StandardAccount stdAccount = new StandardAccount("account one",StandardAccount.AccountType.AT);
//this workds fine
stdAccount.Balance = 123;
//but.. cannot add the account to my book
//ERROR - System.NullReferenceException: 'Object reference not set to an instance of an object.'
myAccounting.Book.Accounts.Add(stdAccount);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)