2010-07-27 3 views
0

다음은 콜렉션이있는 사용자 정의 클래스입니다. 내 응용 프로그램은 여러 프로그램에 대해 여러 번 "프로그램"양식을 열 수있는 MDI 응용 프로그램이며 ProgramBudget 클래스를 사용하려고하지만 프로그램 예산 내의 모든 "하위 클래스"가 프로그램 양식 ProgramBudget 내에 포함되기를 원합니다. 수업.C# 사용자 정의 클래스 콜렉션

나는 서브 클래스가 recursivelly 인텔리에서 참조하기 계속하지만 문제가 있어요 (예 : ProgramBudget.ParticipantTypeBudget.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

정말 그 코드를 모두 볼 필요가 있었습니까? 의문의 여지가없이? 그래서 내가 너의 질문에 동의하지 않았다. –

+7

누군가 자동 속성에 대해 알아야합니다. – Aren

+1

진지하게 - 자동 속성을 조사하십시오. 그것은 당신에게 코딩 톤을 저장합니다. –

답변

6

ParticipantTypeBudgetProgramBudget의 구성원입니다. ParticipantTypeBudgetProgramBudget을 상속합니다. 즉, 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(); 

이 재귀 제거하기 ProgramBudget에서 ParticipantTypeBudget 등 이동합니다.

편집 : 또한 자동 속성에 대해 질문했습니다. 이들은 명시적인 배경 필드가없는 속성입니다. 나는이 패턴을 사용 : 읽기/쓰기 속성에 대해

을, 이렇게 :

public int MyProperty { 
    get; 
    set; 
} 

참고 그 모두 얻을 세트는 본문을 가질 수 없습니다.

당신이 읽기 전용 속성 만 객체 자체에 의해 설정 될 수있다 즉, 하나를해야 할 경우, 세트 구성원을 보호합니다

public int MyProperty { 
    get; 
    protected set; 
} 
+0

자동 속성 예를 추가했습니다. –

+0

그게 전부 자동 속성 무엇입니까? 꽤 쉬워. – mattgcon

+0

. 멋지고 쉽습니다. 다만 기억하십시오 : 당신이 얻거나 놓기 위하여 몸을 추가하는 경우에, 당신은 다른 것을 또한 실행해야한다. –

2

ProgramBudget의 서브 클래스, 모든 중첩 된 클래스를 포함한 모든 ProgramBudget의 회원, 상속 - 따라서, 자신을 포함.

그러나 진짜 질문은 다음과 같습니다. 왜 모두 정확히 중첩 클래스가되기를 원하십니까? 그게 당신에게 어떤 유익을 줍니까?

+0

OP가 그 중첩 된 클래스들 모두를 쓰지 않았다고 의심합니다. –

+0

Fyodor, 제 의도는 ProgramBudget 객체 안에 모든 것을 포함시키는 것이 었습니다. 양식 내의 ProgramBudget 객체에서 모든 것을 액세스 할 수 있기를 바랍니다. 이것은 심지어 가능하거나 올바른 코딩입니까? – mattgcon

+0

네, 저는 모두가 ProgramBudger에 있기를 원한다는 것을 이해합니다. 하지만 문제는 그러한 합의에서 얻을 수있는 혜택은 무엇입니까? –

0

파생 형식이 기본 형식의 자식이므로 무한 재귀적인 부모 - 자식 관계가 만들어집니다. 무한 중첩을 제거하기 위해 파생 된 자식 클래스를 자체 클래스로 이동합니다.

0

내가 당신의 시간 모두를 낭비 모두 죄송합니다. 나는 아팠고 나는이 수업을 만들 때 명확하게 생각하지 못했습니다. 나는 지금 반복적 인 것을 완전히 이해하고 있으며, ProgramBudget 클래스 외부의 각 하위 클래스에 대한 커스텀 클래스를 생성하고 각각의 하위 클래스에 대한 ProgramBudget 내에서 속성을 생성해야한다. 나는 자동차 속성을보고 그 개념을 활용할 것입니다.