【发布时间】:2021-02-24 15:02:00
【问题描述】:
我想就我面临的一个问题向专家请教。在自下而上的方法中,我有一个 StandardAccount 类,它使用几个简单的属性进行初始化,其中一个是枚举(以及一个在实例化时自动设置的 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)。我设置我的书类如下:
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; }
}
我有这两个错误让我无法继续前进,我不明白它们(因为我试图在“母亲”书类而不是 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);
【问题讨论】:
-
为什么需要这个功能?如果您需要迭代帐户,请执行以下操作:
foreach (var account in book.Accounts) -
一本书不是作者的集合。它有作者。拥有一个返回不相关实体的
GetEnumerator会让每个人都感到困惑,即使是作者(你)也会在一段时间后感到困惑 -
你为什么首先尝试实现
IEnumerable?您的List已经具备这些功能。 -
对于另一个错误,您需要添加一个
Account的新实例,而不仅仅是两个属性。但同样,不需要该方法,因为List属性已经拥有它。 -
@Nick 每个人的意思是,将
Book视为Author对象的容器是非常不寻常且几乎总是错误的。如果您需要在书中添加标签和关键字怎么办?还是章节?为什么迭代一本书会返回作者而不是章节?
标签: c# ienumerable