2010-07-27 85 views
0

以下是我的自定義類,包含集合。我的應用程序是一個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]; } 
     } 
    } 
} 
+0

我們是否真的需要查看所有代碼?毫無疑問?這就是爲什麼我低估了你的問題。 – 2010-07-27 19:28:27

+7

有人需要了解自動屬性! – Aren 2010-07-27 19:28:44

+1

認真 - 看看自動屬性。它將爲您節省大量的編碼。 – 2010-07-27 19:30:22

回答

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; 
} 
+0

我爲你添加了自動屬性的例子。 – 2010-07-27 19:47:12

+0

這就是所有的汽車屬性?這很容易 – mattgcon 2010-07-27 20:04:45

+0

是的。好,易於。請記住:如果你添加一個body來獲取或設置,你必須實現另一個。 – 2010-07-27 20:16:37

2

由於ParticipantTypeBudgetProgramBudget一個子類,它繼承所有ProgramBudget的成員,包括所有嵌套類 - 因此,包括其本身。

然而,真正的問題是:爲什麼你要他們都是嵌套類?這會給你帶來什麼好處?

+0

我懷疑OP沒有自己編寫所有的嵌套類 – 2010-07-27 19:31:52

+0

Fyodor,我的意圖是讓所有的東西都包含在ProgramBudget對象中。我希望可以從表單中的ProgramBudget對象訪問所有內容。這是甚至可能或正確的編碼? – mattgcon 2010-07-27 19:37:53

+0

是的,我知道你希望他們都在ProgramBudger中。但問題是:你希望從這種安排中得到什麼好處? – 2010-07-27 19:46:52

0

您的派生類型是它們基類型的子類,因此創建了無限遞歸的父 - 子關係。將派生的子類移到它們自己的獨立類中,以消除無限嵌套。

0

我很抱歉,大家浪費你的時間每一個人。我一直生病,而且當我創作這門課時,我並沒有清楚地思考。我完全理解現在的復發事件,並且我應該爲ProgramBudget類之外的每個子類創建自定義類,併爲每個子類在ProgramBudget中創建一個屬性。我將着眼於汽車性能並利用這一概念。