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

View File

@ -38,7 +38,6 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
} }
} }
} }
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
@ -69,7 +68,6 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
} }
return -1; return -1;
} }
public bool Insert(T obj, int position) public bool Insert(T obj, int position)
{ {
if (position < 0 || position >= _collection.Length) // проверка позиции if (position < 0 || position >= _collection.Length) // проверка позиции
@ -97,14 +95,12 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
} }
return false; return false;
} }
public T? Remove(int position)
public T Remove(int position)
{ {
if (position < 0 || position >= _collection.Length || _collection[position]==null) // проверка позиции и наличия объекта if (position < 0 || position >= _collection.Length || _collection[position]==null) // проверка позиции и наличия объекта
return null; return null;
T temp = _collection[position]; T? temp = _collection[position];
_collection[position] = null; _collection[position] = null;
return temp; 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) public ShipDocks(int picWidth, int picHeight, ICollectionGenericObjects<DrawningShip> collection) : base(picWidth, picHeight, collection)
{ {
} }
protected override void DrawBackground(Graphics g) protected override void DrawBackground(Graphics g)
{ {
Pen pen = new Pen(Color.Brown, 4); Pen pen = new Pen(Color.Brown, 4);
@ -40,7 +39,6 @@ public class ShipDocks : AbstractCompany
x = 0; x = 0;
} }
} }
protected override void SetObjectsPosition() protected override void SetObjectsPosition()
{ {
int count = 0; int count = 0;

View File

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