Answer the question
In order to leave comments, you need to log in
Why doesn't it add an element to the list?
The task is to add user data to the class fields, and then the class object itself to the list.
public List<Meetings> meet = new List<Meetings>();
public void CreateMeet()
{
Console.WriteLine("Введите дату встречи:");
string dateMeeting = Console.ReadLine();
Console.WriteLine("Введите время начала встречи:");
string startMeeting = Console.ReadLine();
Console.WriteLine("Введите время конца встречи:");
string endMeeting = Console.ReadLine();
meet.Add(new Meetings { DateMeeting = dateMeeting, StartMeeting = startMeeting, EndMeeting = endMeeting});
Console.WriteLine("Встреча добавлена! \n");
public void ShowMeetings()
{
foreach (Meetings meet in meet)
{
Console.WriteLine($"Дата встречи: {meet.DateMeeting}, Начало встречи: {meet.StartMeeting}, Конец встречи: {meet.EndMeeting}");
}
Console.WriteLine();
}
meet.Add(new Meetings { DateMeeting = "2019-01-21", StartMeeting = "12:31", EndMeeting = "12:32" });
foreach (Meetings meet in meet)
{
Console.WriteLine($"Дата встречи: {meet.DateMeeting}, Начало встречи: {meet.StartMeeting}, Конец встречи: {meet.EndMeeting}");
}
Answer the question
In order to leave comments, you need to log in
As already correctly pointed out, the problem is that you are creating a new ManagerMeeting instance each time :
public void Route(string answer)
{
var manager = new ManagerMeeting();
class Meetings // Класс встреч
{
public string DateMeeting { get; set; }
public string StartMeeting { get; set; }
public string EndMeeting { get; set; }
public override string ToString()
{
return $"Дата встречи: {DateMeeting}, Начало встречи: {StartMeeting}, Конец встречи: {EndMeeting}";
}
}
class ManagerMeeting // Класс управление встречами
{
public List<Meetings> MeetingList { get; } = new List<Meetings>();
public void Add(Meetings meet)
{
MeetingList.Add(meet);
}
public string FormatMeetings()
{
StringWriter sw = new StringWriter();
foreach (var meet in MeetingList)
sw.WriteLine(meet);
return sw.ToString();
}
}
class Program
{
static ManagerMeeting manager = new ManagerMeeting();
public static void ShowMainMenu()
{
Console.WriteLine("Выберите действие: \n1. Показать все встречи\n2. Создать новую встречу");
}
public static bool Handler()
{
Console.Clear();
ShowMainMenu();
string answer = Console.ReadLine();
switch (answer)
{
case "1":
var result = manager.FormatMeetings();
if (string.IsNullOrWhiteSpace(result))
Console.WriteLine("Список пуск");
else
Console.WriteLine(manager.FormatMeetings());
return true;
case "2":
manager.Add(ReadMeeting());
return true;
default:
return false;
}
}
public static void Main(string[] args)
{
while (Handler()) Console.ReadKey(true);
Console.ReadKey(true);
}
public static Meetings ReadMeeting()
{
Console.WriteLine("Введите дату встречи:");
var date = Console.ReadLine();
Console.WriteLine("Введите время начала встречи:");
var startMeeting = Console.ReadLine();
Console.WriteLine("Введите время конца встречи:");
var endMeeting = Console.ReadLine();
Console.WriteLine("Встреча добавлена! \n ");
//Для проверки, записываются ли поля - записываются
Console.WriteLine(date + startMeeting + endMeeting);
return new Meetings
{
DateMeeting = date,
StartMeeting = startMeeting,
EndMeeting = endMeeting
};
}
}
You are probably recreating the meet list somewhere. Put readonly on the meet field (better yet, rename it to MeetingsList)
This will prevent accidental overwriting of the variable.
>foreach (Meetings meet in meet)
There is an error in the code, you cannot create a loop counter variable Meetings meet
, because there is already a variable named meet. If an error does not occur here, then, most likely, in one of the two names, instead of the English "e", there is a Russian "e".
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question