C#自定义类集合混乱

时间:2010-07-27 19:24:51

标签: c# class

以下是我的自定义类,包含集合。我的应用程序是一个MDI应用程序,能够为多个程序多次打开“程序”表单,我想使用ProgramBudget类,但我希望程序预算中的所有“子类”都包含在Program表单ProgramBudget中类。

我遇到了问题,但子类继续在IntelliSense中被引用(即: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]; }
        }
    }
}

4 个答案:

答案 0 :(得分:6)

ParticipantTypeBudgetProgramBudget的成员。 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;
}

答案 1 :(得分:2)

由于ParticipantTypeBudgetProgramBudget的子类,它继承了所有ProgramBudget的成员,包括所有嵌套类 - 因此,包括它自己。

但真正的问题是:为什么你希望它们都是嵌套类?这给你带来了什么好处?

答案 2 :(得分:0)

您的派生类型是其基类型的子类,从而创建无限递归的父子关系。将派生的子类移动到它们自己的独立类中,以消除无限的嵌套。

答案 3 :(得分:0)

我为每个人浪费你的时间而道歉。我生病了,在创建这门课时我并没有想清楚。我现在完全理解了recurvsive的事情,我应该为ProgramBudget类之外的每个子类创建自定义类,并在ProgramBudget中为每个子类创建一个属性。我将查看汽车属性并使用该概念。