Привет, Итак, мне нужно разработать программу, которая может добавить в список для моей курсовой документации доставку или доставку для меня, я сделал список, называемый посещением, как показано ниже.Как использовать только один список
Я хотел бы знать, как добавить каждый пикап или доставку в список таким образом, чтобы он мог различать то, что было изначально, чтобы я мог показывать только пикапы или только доставку.
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 CODE
/*
* 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
Обычно у вас будет два списка: один для пикапов и один для поставок. Имеет смысл иметь единственный список «посещений», когда вам не нужно различать типы посещений (т. Е. Вы просто касаетесь вещи в классе «Посещение», даже если методы имеют разные (полиморфные) реализации в каждом конкретном типе). – Cameron
Хорошо. В этом случае у меня есть одна кнопка для доставки и одна для Pickups, которая добавляет их в listBox, чтобы показать сводку ... как я мог бы реализовать кнопку для их сортировки, если кнопка только для отправки шоу или только для доставки – TAM