перегрузка сложения и вычитания

This commit is contained in:
bekodeg 2023-10-25 09:50:26 +04:00
parent 7880fc1215
commit ddcceff03f
2 changed files with 13 additions and 10 deletions

View File

@ -56,14 +56,17 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static bool operator +(LocomotivesGenericCollection<T, U> collect, T?
obj)
public static int operator +(LocomotivesGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return false;
return 0;
}
return collect?._collection.Insert(obj) ?? false;
if (collect?._collection.Insert(obj) ?? false)
{
return collect._collection.Count;
}
return 0;
}
/// <summary>
/// Перегрузка оператора вычитания
@ -71,15 +74,15 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static T? operator -(LocomotivesGenericCollection<T, U> collect, int
public static int operator -(LocomotivesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
if (obj != null && collect._collection.Remove(pos))
{
collect._collection.Remove(pos);
return collect._collection.Count;
}
return obj;
return -1;
}
/// <summary>
/// Получение объекта IMoveableObject

View File

@ -41,7 +41,7 @@ namespace ProjectElectricLocomotive
FormLocomotive form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_locomotives + form.SelectedLocomotive)
if (_locomotives + form.SelectedLocomotive > 0)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _locomotives.ShowLocomotives();
@ -66,7 +66,7 @@ namespace ProjectElectricLocomotive
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_locomotives - pos != null)
if (_locomotives - pos != -1)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _locomotives.ShowLocomotives();