2012-11-23 4 views
0

안녕하세요. 저는 회사의 Pickups 또는 Delivery를 내 교과목 목록에 추가 할 수있는 프로그램을 디자인해야합니다. 아래 목록과 같이 방문이라는 목록을 만들었습니다.하나의 목록 만 사용하는 방법

각 픽업이나 배송을 목록에 추가하여 원래 픽업을 구분할 수있는 방법을 알고 싶습니다. 픽업 또는 배송 만 표시 할 수 있습니다.

class List 
{ 
/* 
* This object represents the List. It has a 1:M relationship with the Visit class 
*/ 

    private List<Visits> visits = new List<Visits>(); 
    //List object use to implement the relationshio with Visits 

    public void addVisits(Visits vis) 
    { 
     //Add a new Visit to the List 
     visits.Add(vis); 
    } 

    public List<String> listVisits() 
    {//Generate a list of String objects, each one of which represents a Visit in List. 

     List<String> listVisits = new List<string>(); 
     //This list object will be populated with Strings representing the Visits in the lists 

     foreach (Visits vis in visits) 
     { 
      String visAsString = vis.ToString(); 
      //Get a string representing the current visit object 

      listVisits.Add(visAsString); 
      //Add the visit object to the List 
     } 

     return listVisits; 
     //Return the list of strings 
    } 

    public Visits getVisits(int index) 
    { 
     //Return the visit object at the <index> place in the list 

     int count = 0; 
     foreach (Visits vis in visits) 
     { 
      //Go through all the visit objects 
      if (index == count) 
       //If we're at the correct point in the list... 
       return vis; 
      //exit this method and return the current visit 
      count++; 
      //Keep counting 
     } 
     return null; 
     //Return null if an index was entered that could not be found 
    } 

는 SHOW 코드

 /* 
     * Update the list on this form the reflect the visits in the list 
     */ 
     lstVisits.Items.Clear(); 
     //Clear all the existing visits from the list 

     List<String> listOfVis = theList.listVisits(); 
     //Get a list of strings to display in the list box 


     lstVisits.Items.AddRange(listOfVis.ToArray()); 
     //Add the strings to the listBox. Note to add a list of strings in one go we have to 
     //use AddRange and we have to use the ToArray() method of the list that we are adding 
+0

일반적으로 두 개의 목록이 있습니다. 하나는 픽업을위한 것이고 다른 하나는 배달을위한 것입니다. 방문 유형을 구별 할 필요가 없을 때 (즉 방문 클래스에서 물건을 만지는 경우, 즉 메소드가 각 구체 유형에서 다른 (다형성) 구현을 가지고 있더라도 하나의 "방문"목록을 갖는 것이 맞습니다.). – Cameron

+0

좋아 그럼 그 경우에는 배달을위한 버튼 하나와 요약을 보여주기 위해 listBox에 추가하는 픽업을위한 버튼 하나가 있습니다 ... 어떻게 픽업 전용 버튼이 있는지 또는 배송에만 맞는지 버튼을 구현할 수 있습니까 – TAM

답변

1

이었다 유형을 검색 할 수 있습니다.

알터 Visits은 다음과 같이 예를 들어, 방문의 종류를 구별 할 수있는 방법을 제공합니다 :

enum VisitKind { 
    Pickup, 
    Delivery 
} 

public class Visits { 
    public VisitKind KindOfVisit {get;private set;} 
    protected Visits(VisitKind kind) { 
     KindOfVisit = kind; 
    } 
} 

지금 자신의 수퍼 클래스의 생성자에 올바른 종류를 전달하는 PickupDelivery의 생성자를 수정, 같은 :

이제
public class Pickup : Visits { 
    public Pickup() : base(VisitKind.Pickup) {} 
} 
public class Delivery : Visits { 
    public Delivery() : base(VisitKind.Delivery) {} 
} 

당신은 V의 종류를 이야기하기 위해 PickupDelivery에 공통 KindOfVisit 속성을 검사 할 수 있습니다 런타임에 isit 하 : 누군가가 방문의 새로운 종류, 평소와 같이 컴파일 계속 방문을 처리하는 코드를 추가 할 때 때문에

public IList<Delivery> GetDeliveries() { 
    var res = new List<Delivery>(); 
    foreach (var v in visits) { 
     if (v.KindOfVisit == VisitKind.Delivery) { 
      res.Add((Delivery)v); 
     } 
    } 
    return res; 
} 

이 솔루션은 적합하지 않습니다. 이 문제에 대한 해결책은 속성을 추가하는 것보다 훨씬 어렵습니다. 하나의 해결책은 을 사용하는 것이고 이는 단순한 Kind 속성보다 좋지만 자체적으로 제한이 있습니다.


* 명사의 복수와 클래스를 명명하는 것은 틀에 얽매이지 않는입니다 - Visit 아마 더 좋은 이름이 될 것입니다.

+0

확인, 하지만 위의 코드를 사용하면 픽업 목록 만 표시하는 방법은 무엇입니까? 나는 그 부분을 정말로 얻지 못한다 – TAM

+0

@TAM 댓글에서 질문에 대한 대답을 알아 냈는가? – dasblinkenlight

+0

아니요,하지만 원래 게시글에 현재 쇼 코드를 추가 한 다음 .visitkind = delivery 또는 그 무엇이 되겠습니까? – TAM

0

는 픽업 또는 배송 인 경우 추적 유지 귀하의 방문 클래스에 매개 변수를 추가하고 방문수 클래스의 getType로()/setType() 메소드를 추가 당신이 모두 PickupDelivery*Visits라는 공통의 클래스를 상속이처럼 보이는

관련 문제