Sub pageload() Handles Me.Load Dim bom As New List(Of Car) Dim car1 As New Car With {.Name = "Pea", .Year = 2} Dim car2 As New Car With {.Name = "Pea", .Year = 2} bom.Add(car1) MsgBox(bom.Contains(car2)) End Sub
WHY??? I mean, the object has exactly the same data, so why does it say that it is not contained?
The method Containschecks equality by calling the method Equalson objects. If you do not override the method Equalsin the class Carand do not implement your own means of comparison, the instances Carwill be considered equal by means of a reference identifier (which is the exact same object), not based on the fact that their contents are equal.
Contains
Equals
Car
, , . : .
, car , . - . , . . . , , - , .
car
car2, car1 (Dim car2 As New Car = car1), . . .
car2
Dim car2 As New Car = car1
, , ( . ). , , , 10 , , 10 . - , , 10 10 .
, , .
Equals , , . ( Equals.) , .
Your two cars are completely independent of each other.
Containschecks if any list item indicates the same object as the parameter you provided. And aren't these two objects logical.
If you want to do this, you must override the - Equalsmethod of yourCar
Source: https://habr.com/ru/post/1733257/More articles:Problem with selectors - javascriptManipulate HTML from Firefox Extension - firefoxBuying Microsoft SQL Server 2008 Web Edition - sqlКак мне заполнить поля города/штата на основе zip? - language-agnosticРазвертывание Java-приложения как сервлета - javaКак я могу захватить группу цифр в конце строки в Perl? - regexJava EE and GWT authentication - java-eeUnable to use collections with InExpression - nhibernateSphinx and word forms - sphinxGet a list of available drives and their sizes - cAll Articles