Cравнение двух моделей - C#

Узнай цену своей работы

Формулировка задачи:

Листинг программы
  1. internal class StructuresAlone
  2. {
  3. public string Fio { set; get; }
  4. public int Pol { set; get; }
  5. public DateTime DateRo { set; get; }
  6. public DateTime DateSm { set; get; }
  7. public string Country { set; get; }
  8. public string Street { set; get; }
  9. public string Phone { set; get; }
  10. public string PlaceWork { set; get; }
  11. public string SemPol { set; get; }
  12. }
Одну инициализирую при загрузке формы:
Листинг программы
  1. private StructuresAlone _alone;
  2. private void AloneLoad()
  3. {
  4. _alone = new StructuresAlone();
  5. ...заполнение....
  6. }
  7. private void tabControl1_Deselecting(object sender, TabControlCancelEventArgs e){
  8. var structuresAlone = new StructuresAlone
  9. {
  10. Country = comboBox2.Text,
  11. DateRo = dateTimePicker1.Value,
  12. DateSm = dateTimePicker2.Value,
  13. Fio = textBox1.Text,
  14. Phone = textBox3.Text,
  15. Street = textBox2.Text,
  16. SemPol = comboBox3.Text,
  17. PlaceWork = textBox12.Text
  18. };
  19. if (radioButton1.Checked)
  20. structuresAlone.Pol = 1;
  21. if (!structuresAlone.Equals(_alone))
  22. {
  23. if (AloneEdit())
  24. {
  25. MessageBox.Show(@"Данные успешно изменены.", @"Успешно", MessageBoxButtons.OK,
  26. MessageBoxIcon.Information);
  27. }
  28. }
  29. }
На сравнении:
Листинг программы
  1. if (!structuresAlone.Equals(_alone))
Он показывает что они не равны. по данным внутри они равные. В чем может быть проблема?

Решение задачи: «Cравнение двух моделей»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication41 {
  7.     class Program {
  8.         static void Main(string[] args) {
  9.             TestObj t1 = new TestObj { X = 10, Y = 20 };
  10.             TestObj t2 = new TestObj { X = 10, Y = 21 };
  11.             Console.WriteLine(t1 == t2);
  12.             Console.ReadLine();
  13.         }
  14.     }
  15.     public class TestObj : IEquatable<TestObj> {
  16.         public int X { get; set; }
  17.         public int Y { get; set; }
  18.  
  19.         public bool Equals(TestObj other) {
  20.             if (other == null) {
  21.                 return false;
  22.             }
  23.             return X == other.X && Y == other.Y;
  24.         }
  25.  
  26.         public override string ToString() {
  27.             return string.Format("X={0}, Y={1}", X, Y);
  28.         }
  29.         public override int GetHashCode() {
  30.             return (X * Y) ^ 127;
  31.         }
  32.         public override bool Equals(object obj) {
  33.             return Equals(obj as TestObj);
  34.         }
  35.         public static bool operator ==(TestObj t1, TestObj t2) {
  36.             if (object.ReferenceEquals(t1, t2)) {
  37.                 return true;
  38.             }
  39.             if ((object)t1 == null || (object)t2 == null) {
  40.                 return false;
  41.             }
  42.             return t1.X == t2.X && t1.Y == t2.Y;
  43.         }
  44.         public static bool operator !=(TestObj t1, TestObj t2) {
  45.             return !(t1 == t2);
  46.         }
  47.     }
  48. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 4.25 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы