C#自定义类收集混淆
以下是我的自定义类,包含集合。我的应用程序是一个MDI应用程序,可以为多个程序多次打开“程序”表单,我希望使用ProgramBudget类,但是我希望程序预算内的所有“子类”都包含在程序表单ProgramBudget中类。C#自定义类收集混淆
我有但是在子类中不断得到智能感知引用recursivelly问题(即:ProgramBudget.ParticipantTypeBudget.ParticipantTypeBudget.ParticipantTypeBudget ...)
什么是错的这个类,我如何使用它,自定义类低于:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IPAM_NSF_Financials.Application_Classes
{
public class ProgramBudget
{
internal string strProgramCode;
private string strActualTravelSourceRefCode;
private string strActualExpenseSourceRefCode;
private string strProgramBudgetComment;
private string strParticipantComment;
private decimal decInitTravel;
private decimal decInitLodging;
private decimal decInitStipend;
private decimal decRemTravel;
private decimal decRemLodging;
private decimal decRemStipend;
private decimal decPartTypeTravel;
private decimal decPartTypeExpenses;
private decimal decPartPrimTravelBudget;
private decimal decPartPrimExpenseBudget;
private decimal decPartPrimRegFee;
private decimal decActualTravelAir;
private decimal decActualTravelGround;
private decimal decActualTravelMisc;
private decimal decActualExpenseLodging;
private decimal decActualExpenseCatering;
private decimal decActualExpenseMisc;
private int nProgramBudgetID;
private int nParticipantTypeBudgetID;
private int nParticipantBudgetHDRID;
private int nParticipantBudgetHDRCommentID;
private int nParticipantBudgetDTLID;
private int nParticipantBudgetDTLExpenseID;
private int nParticipantBudgetDTLTravelID;
private int nPartType;
private int nParticipant;
private int nActualTravelSource;
private int nActualExpenseSource;
public int ProgramBudgetID
{
get { return nProgramBudgetID; }
set { nProgramBudgetID = value; }
}
public string ProgramCode
{
get { return strProgramCode; }
set { strProgramCode = value; }
}
public decimal InitialTravel
{
get { return decInitTravel; }
set { decInitTravel = value; }
}
public decimal InitialLodging
{
get { return decInitLodging; }
set { decInitLodging = value; }
}
public decimal InitialStipend
{
get { return decInitStipend; }
set { decInitStipend = value; }
}
public decimal RemainingTravel
{
get { return decRemTravel; }
set { decRemTravel = value; }
}
public decimal RemainingLodging
{
get { return decRemLodging; }
set { decRemLodging = value; }
}
public decimal RemainingStipend
{
get { return decRemStipend; }
set { decRemStipend = value; }
}
public string ProgramBudgetComment
{
get { return strProgramBudgetComment; }
set { strProgramBudgetComment = value; }
}
public class ParticipantTypeBudget : ProgramBudget
{
public int ParticipantTypeBudgetID
{
get { return nParticipantTypeBudgetID; }
set { nParticipantTypeBudgetID = value; }
}
public int ParticipantType
{
get { return nPartType; }
set { nPartType = value; }
}
public decimal ParticipantTypeTravel
{
get { return decPartTypeTravel; }
set { decPartTypeTravel = value; }
}
public decimal ParticipantTypeExpenses
{
get { return decPartTypeExpenses; }
set { decPartTypeExpenses = value; }
}
}
class ParticipantTypeBudgets : CollectionBase
{
public void Add(ParticipantTypeBudget partTypeBudgetObject)
{ InnerList.Add(partTypeBudgetObject); }
public void Remove(int Index)
{ InnerList.RemoveAt(Index); }
public ParticipantTypeBudget Item(int Index)
{ return (ParticipantTypeBudget)InnerList[Index]; }
}
public class ParticipantPrimaryBudget : ProgramBudget
{
public int ParticipantBudgetHDRID
{
get { return nParticipantBudgetHDRID; }
set { nParticipantBudgetHDRID = value; }
}
public int ParticipantID
{
get { return nParticipant; }
set { nParticipant = value; }
}
public string ParticipantBudgetComment
{
get { return strParticipantComment; }
set { strParticipantComment = value; }
}
public decimal ParticipantPrimaryTravelBudget
{
get { return decPartPrimTravelBudget; }
set { decPartPrimTravelBudget = value; }
}
public decimal ParticipantPrimaryExpenseBudget
{
get { return decPartPrimExpenseBudget; }
set { decPartPrimExpenseBudget = value; }
}
public decimal ParticipantPrimaryRegFee
{
get { return decPartPrimRegFee; }
set { decPartPrimRegFee = value; }
}
}
public class ParticipantActualTravelBudget : ProgramBudget
{
public int ParticipantBudgetDTLID
{
get { return nParticipantBudgetDTLID; }
set { nParticipantBudgetDTLID = value; }
}
public int ParticipantBudgetDTLTravelID
{
get { return nParticipantBudgetDTLTravelID; }
set { nParticipantBudgetDTLTravelID = value; }
}
public int FundingSource
{
get { return nActualTravelSource; }
set { nActualTravelSource = value; }
}
public string ReferenceCode
{
get { return strActualTravelSourceRefCode; }
set { strActualTravelSourceRefCode = value; }
}
public decimal Air
{
get { return decActualTravelAir; }
set { decActualTravelAir = value; }
}
public decimal Ground
{
get { return decActualTravelGround; }
set { decActualTravelGround = value; }
}
public decimal Miscellaneous
{
get { return decActualTravelMisc; }
set { decActualTravelMisc = value; }
}
}
class ParticipantActualTravelBudgets : CollectionBase
{
public void Add(ParticipantActualTravelBudget partActTravelBudgetObject)
{ InnerList.Add(partActTravelBudgetObject); }
public void RemoveAt(int Index)
{ InnerList.RemoveAt(Index); }
public ParticipantActualTravelBudget Item(int Index)
{ return (ParticipantActualTravelBudget)InnerList[Index]; }
}
public class ParticipantActualExpensesBudget : ProgramBudget
{
public int ParticipantBudgetDTLID
{
get { return nParticipantBudgetDTLID; }
set { nParticipantBudgetDTLID = value; }
}
public int ParticipantBudgetDTLExpenseID
{
get { return nParticipantBudgetDTLExpenseID; }
set { nParticipantBudgetDTLExpenseID = value; }
}
public int FundingSource
{
get { return nActualExpenseSource; }
set { nActualExpenseSource = value; }
}
public string ReferenceCode
{
get { return strActualExpenseSourceRefCode; }
set { strActualExpenseSourceRefCode = value; }
}
public decimal Lodging
{
get { return decActualExpenseLodging; }
set { decActualExpenseLodging = value; }
}
public decimal Catering
{
get { return decActualExpenseCatering; }
set { decActualExpenseCatering = value; }
}
public decimal Miscellaneous
{
get { return decActualExpenseMisc; }
set { decActualExpenseMisc = value; }
}
}
class ParticipantActualExpenseBudgets : CollectionBase
{
public void Add(ParticipantActualExpensesBudget partActExpensesBudgetObject)
{ InnerList.Add(partActExpensesBudgetObject); }
public void RemoveAt(int Index)
{ InnerList.RemoveAt(Index); }
public ParticipantActualExpensesBudget Item(int Index)
{ return (ParticipantActualExpensesBudget)InnerList[Index]; }
}
}
}
ParticipantTypeBudget
是ProgramBudget
的成员。 ParticipantTypeBudget
也继承了ProgramBudget
,这意味着它获得了所有ProgramBudget
的非私人成员。
看到问题了吗?
class Foo {
class Bar : Foo {
}
}
//perfectly legal:
Foo.Bar.Bar.Bar.Bar.Bar = new Foo.Bar.Bar.Bar.Bar.Bar.Bar.Bar.Bar.Bar.Bar.Bar.Bar();
摆脱这个递归,移动ParticipantTypeBudget
,等出来的ProgramBudget
。
编辑:另外,你问了汽车性能。那些属性没有明确的后台字段。我使用两种模式:
对于一个读/写性能,你这样做:
public int MyProperty {
get;
set;
}
注意,get和set不能有身体。
如果你需要一个只读属性,即一个只能由对象本身进行设置,使集合成员的保护:
public int MyProperty {
get;
protected set;
}
我为你添加了自动属性的例子。 – 2010-07-27 19:47:12
这就是所有的汽车属性?这很容易 – mattgcon 2010-07-27 20:04:45
是的。好,易于。请记住:如果你添加一个body来获取或设置,你必须实现另一个。 – 2010-07-27 20:16:37
由于ParticipantTypeBudget
是ProgramBudget
一个子类,它继承所有ProgramBudget
的成员,包括所有嵌套类 - 因此,包括其本身。
然而,真正的问题是:为什么你要他们都是嵌套类?这会给你带来什么好处?
我怀疑OP没有自己编写所有的嵌套类 – 2010-07-27 19:31:52
Fyodor,我的意图是让所有的东西都包含在ProgramBudget对象中。我希望可以从表单中的ProgramBudget对象访问所有内容。这是甚至可能或正确的编码? – mattgcon 2010-07-27 19:37:53
是的,我知道你希望他们都在ProgramBudger中。但问题是:你希望从这种安排中得到什么好处? – 2010-07-27 19:46:52
您的派生类型是它们基类型的子类,因此创建了无限递归的父 - 子关系。将派生的子类移到它们自己的独立类中,以消除无限嵌套。
我很抱歉,大家浪费你的时间每一个人。我一直生病,而且当我创作这门课时,我并没有清楚地思考。我完全理解现在的复发事件,并且我应该为ProgramBudget类之外的每个子类创建自定义类,并为每个子类在ProgramBudget中创建一个属性。我将着眼于汽车性能并利用这一概念。
我们是否真的需要查看所有代码?毫无疑问?这就是为什么我低估了你的问题。 – 2010-07-27 19:28:27
有人需要了解自动属性! – Aren 2010-07-27 19:28:44
认真 - 看看自动属性。它将为您节省大量的编码。 – 2010-07-27 19:30:22