Нужно сравнить объект класса Planshet с объектами списка класса Planshet. Но при попытке в методе SearchPlanchet() с одинаковыми параметрами все равно выходит False. Заранее прошу прощения за быдлокод.
using System;
using System.Collections.Generic;
namespace PlanshestsShop
{
class Program
{
static void Main(string[] args)
{
Shop.BuyPlanshet();
Shop.SearchPlanchet(Planshets._planshetList);
}
}
public class Planshets
{
public static List<Planshet> _planshetList = new List<Planshet>(5)
{
new Planshet( Planshet.ModelsList.Samsung, Planshet.ColorCollection.Black, 2012, 1500),
new Planshet( Planshet.ModelsList.Apple, Planshet.ColorCollection.Black, 2008, 2000),
new Planshet( Planshet.ModelsList.Huawei, Planshet.ColorCollection.Blue, 2016, 4000),
new Planshet( Planshet.ModelsList.Samsung, Planshet.ColorCollection.White, 2018, 5000),
new Planshet( Planshet.ModelsList.Huawei, Planshet.ColorCollection.Black, 2020, 6000)
};
}
public class Planshet
{
public enum ModelsList { Samsung, Huawei, Apple }
public ModelsList Model { get; set; }
public enum ColorCollection { White, Black, Blue }
public ColorCollection Color { get; set; }
public int Year { get; set; }
public int Price { get; set; }
public override string ToString()
{
return string.Format("Модель: {0} \nКолір: {1} \nРік: {2} \nЦіна: {3}", Model, Color, Year, Price);
}
public Planshet(ModelsList model, ColorCollection color, int year, int price)
{
Model = model;
Color = color;
Year = year;
Price = price;
}
}
public class Shop
{
static Planshet UserHotelka = new Planshet(Planshet.ModelsList.Samsung, Planshet.ColorCollection.Black, 0, 0);
public static Planshet BuyPlanshet()
{
Console.WriteLine("Модель:");
Console.WriteLine("0 - Samsung\t 1 - Huawei\t2 - Apple");
int Choose = 0;
Choose = Convert.ToInt32(Console.ReadLine());
switch (Choose)
{
case 0: UserHotelka.Model = Planshet.ModelsList.Samsung; break;
case 1: UserHotelka.Model = Planshet.ModelsList.Huawei; break;
case 2: UserHotelka.Model = Planshet.ModelsList.Apple; break;
}
Console.WriteLine("Цвет:");
Console.WriteLine("0 - Белый\t 1 - Черный\t2 - Синий");
Choose = Convert.ToInt32(Console.ReadLine());
switch (Choose)
{
case 0: UserHotelka.Color = Planshet.ColorCollection.White; break;
case 1: UserHotelka.Color = Planshet.ColorCollection.Black; break;
case 2: UserHotelka.Color = Planshet.ColorCollection.Blue; break;
}
Console.WriteLine("Год:");
UserHotelka.Year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Цена:");
UserHotelka.Price = Convert.ToInt32(Console.ReadLine());
return UserHotelka;
}
public static void SearchPlanchet(List<Planshet> _planshetList)
{
for (int i = 0; i < _planshetList.Count; i++)
{
bool result = ValueType.Equals(_planshetList[i], UserHotelka);
if (result)
{
Console.WriteLine("aaa");
}
else
{
Console.WriteLine("Пусто");
break;
}
}
}
}
}
ValueType.Equal
вам не подходит, так как оно значимые типы сравнивает, а у вас ссылочные