Решение проблемы с параметром, указывающим на null

This commit is contained in:
grishazagidulin 2024-04-01 12:15:09 +04:00
parent 417cd36b6c
commit 313b9ea2cf
4 changed files with 3 additions and 24 deletions

View File

@ -16,12 +16,10 @@ public interface ICollectionGenericObjects<T>
/// Количество объектов в коллекции
/// </summary>
int Count { get; }
/// <summary>
/// Установка максимального количества элементов
/// </summary>
int SetMaxCount { set; }
/// <summary>
/// Добавление объекта в коллекцию
/// </summary>
@ -35,14 +33,12 @@ public interface ICollectionGenericObjects<T>
/// <param name="position">Позиция</param>
/// <returns>true - удачно, false - вставка не удалась</returns>
bool Insert(T obj, int position);
/// <summary>
/// Удаление объекта из коллекции с конкретной позиции
/// </summary>
/// <param name="position">Позиция</param>
/// <returns>true - удачно, false - удаление не удалось</returns>
T Remove(int position);
T? Remove(int position);
/// <summary>
/// Получение объекта по позиции
/// </summary>

View File

@ -38,7 +38,6 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
}
}
}
/// <summary>
/// Конструктор
/// </summary>
@ -69,7 +68,6 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
}
return -1;
}
public bool Insert(T obj, int position)
{
if (position < 0 || position >= _collection.Length) // проверка позиции
@ -97,14 +95,12 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
}
return false;
}
public T Remove(int position)
public T? Remove(int position)
{
if (position < 0 || position >= _collection.Length || _collection[position]==null) // проверка позиции и наличия объекта
return null;
T temp = _collection[position];
T? temp = _collection[position];
_collection[position] = null;
return temp;
}
}

View File

@ -22,7 +22,6 @@ public class ShipDocks : AbstractCompany
public ShipDocks(int picWidth, int picHeight, ICollectionGenericObjects<DrawningShip> collection) : base(picWidth, picHeight, collection)
{
}
protected override void DrawBackground(Graphics g)
{
Pen pen = new Pen(Color.Brown, 4);
@ -40,7 +39,6 @@ public class ShipDocks : AbstractCompany
x = 0;
}
}
protected override void SetObjectsPosition()
{
int count = 0;

View File

@ -78,7 +78,6 @@ public partial class FormShipCollection : Form
MessageBox.Show("Не удалось добавить объект");
}
}
/// <summary>
/// Получение цвета
/// </summary>
@ -94,19 +93,16 @@ public partial class FormShipCollection : Form
}
return color;
}
private void ButtonDelShip_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
return;
}
int pos = Convert.ToInt32(maskedTextBox.Text);
if (_company - pos != null)
{
@ -118,14 +114,12 @@ public partial class FormShipCollection : Form
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonGoToTest_Click(object sender, EventArgs e)
{
if (_company == null)
{
return;
}
DrawningShip? ship = null;
int counter = 100;
while (ship == null)
@ -137,20 +131,16 @@ public partial class FormShipCollection : Form
break;
}
}
if (ship == null)
{
return;
}
FormBattleship form = new()
{
SetShip = ship
};
form.ShowDialog();
}
private void ButtonRefresh_Click(object sender, EventArgs e)
{
if (_company == null)
@ -161,6 +151,5 @@ public partial class FormShipCollection : Form
pictureBox.Image = _company.Show();
}
private void ButtonAddBattleship_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBattleship));
private void buttonAddShip_Click_1(object sender, EventArgs e) => CreateObject(nameof(DrawningShip));
}