|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Equality Test For ObjectsI have two objects that I need to compare the values of. The declaration of
the object is: Dim mylist As New Collections.Generic.List(Of Integer) How can I compare the values of the Integers in the List? Thanks. On Thu, 28 May 2009 23:48:16 -0400, "Nathan Sokalski"
<njsokal***@hotmail.com> wrote: >I have two objects that I need to compare the values of. The declaration of First you need to define what equality means.>the object is: > >Dim mylist As New Collections.Generic.List(Of Integer) > >How can I compare the values of the Integers in the List? Thanks. If one list contains: 5, 9, 2 and the other contains: 2, 5, 9 are the lists equal? I guess that's a good point in the case of some objects. In my case, because
I am comparing Lists, they would be equal. Show quoteHide quote "Jack Jackson" <jjackson-***@cinnovations.net> wrote in message news:tbnu15906fh3sa6ev3qr3326grvurrohmb@4ax.com... > On Thu, 28 May 2009 23:48:16 -0400, "Nathan Sokalski" > <njsokal***@hotmail.com> wrote: > >>I have two objects that I need to compare the values of. The declaration >>of >>the object is: >> >>Dim mylist As New Collections.Generic.List(Of Integer) >> >>How can I compare the values of the Integers in the List? Thanks. > > First you need to define what equality means. > > If one list contains: 5, 9, 2 > and the other contains: 2, 5, 9 > > are the lists equal? On Thu, 28 May 2009 21:17:04 -0700, Nathan Sokalski
<njsokal***@hotmail.com> wrote: > I guess that's a good point in the case of some objects. In my case, Lists are ordered. So, while that definition of equality is usable and > because > I am comparing Lists, they would be equal. can be implemented, it's not really the intuitive idea of what it would mean for two List(Of Integer) instances to be equal. You seem to really be treating them as a set, not an ordered list. If that's really the case, you may prefer to store your data as HashSet(Of Integer) instances instead. If you do that, you can use the HashSet.SetEquals() method to compare them for equality. A less-efficient alternative would be to use the same SequenceEquals() method I mentioned before, but apply the OrderBy() method to each List(Of Integer) instance first. For example: If mylist1.OrderBy(Function(i) i).SequenceEquals(mylist2.OrderBy(Function(i) i)) Then ' etc. End If Pete Peter,
I think you are right but why not \\\VB10 code Dim lst1 As New List(Of Integer) From {1, 2, 3, 4} Dim lst2 As New List(Of Integer) From {1, 2, 3, 5} For i = 0 To lst1.Count - 1 If lst1(i) <> lst2(i) Then MessageBox.Show((i + 1).ToString) Exit For Next /// In VB9 the demo list part has to be initialized with two extra lines Cor On Thu, 28 May 2009 23:49:22 -0700, Cor Ligthert[MVP]
<Notmyfirstn***@planet.nl> wrote: > Peter, The above implements the solution when the lists are required to have the > > I think you are right but why not > > \\\VB10 code > Dim lst1 As New List(Of Integer) From {1, 2, 3, 4} > Dim lst2 As New List(Of Integer) From {1, 2, 3, 5} > For i = 0 To lst1.Count - 1 > If lst1(i) <> lst2(i) Then MessageBox.Show((i + 1).ToString) > Exit For > Next elements in the same order. As for "why not" that approach, there's nothing wrong with it per se, but it's not as concise as simply calling SequenceEquals(), and has zero advantages over using that method. I prefer to use the built-in .NET features, especially when they address my need exactly. YMMV. Pete There is nothing about your question specific to C#, and by definition a
post on-topic in a VB newsgroup is off-topic in a C# newsgroup, and vice a versa. Follow-ups modified appropriately. Please don't cross-post inappropriately in the future. As for the question... On Thu, 28 May 2009 20:48:16 -0700, Nathan Sokalski <njsokal***@hotmail.com> wrote: > I have two objects that I need to compare the values of. The declaration I thought you said you had two objects. That's only one.> of > the object is: > > Dim mylist As New Collections.Generic.List(Of Integer) > How can I compare the values of the Integers in the List? Thanks. Compare them how? To each other? To members of some other list?If you want to compare two different instances of List(Of Integer) to make sure they both contain exactly the same elements in exactly the same order, then you can use the Enumerable.SequenceEqual() extension method. For example: If mylist1.SequenceEqual(mylist2) Then ' do whatever End If If that's not what you want to do, you need to be more specific about your question. Pete I do have two objects, I just posted the declaration for one because both
objects have the same declaration (well, other than the name). Sorry about that. I want to compare them to the other List, basically meaning I want to make sure both Lists contain the same values. I think that in my case Enumerable.SequenceEqual() will work, although it would be nice to have a similar method that does not care about the order of the elements. Thanks. Also, and this question is a little bit less urgent, but is there a way to do the same thing for an array of Lists, such as the following: Dim mylist(4) As New Collections.Generic.List(Of Integer) Dim mylist2(4) As New Collections.Generic.List(Of Integer) In my case, I can obviously just call Enumerable.SequenceEqual() for each index to create a Boolean expression, since my arrays are a small fixed size, but in the case of larger or variable sized arrays, this would require a loop or function. Is there a way to do this for arrays without using a loop or function? Thanks again. Show quoteHide quote "Peter Duniho" <NpOeStPe***@nnowslpianmk.com> wrote in message news:op.uuoayix68jd0ej@macbook-pro.local... > There is nothing about your question specific to C#, and by definition a > post on-topic in a VB newsgroup is off-topic in a C# newsgroup, and vice a > versa. > > Follow-ups modified appropriately. Please don't cross-post > inappropriately in the future. > > As for the question... > > On Thu, 28 May 2009 20:48:16 -0700, Nathan Sokalski > <njsokal***@hotmail.com> wrote: > >> I have two objects that I need to compare the values of. The declaration >> of >> the object is: >> >> Dim mylist As New Collections.Generic.List(Of Integer) > > I thought you said you had two objects. That's only one. > >> How can I compare the values of the Integers in the List? Thanks. > > Compare them how? To each other? To members of some other list? > > If you want to compare two different instances of List(Of Integer) to make > sure they both contain exactly the same elements in exactly the same > order, then you can use the Enumerable.SequenceEqual() extension method. > For example: > > If mylist1.SequenceEqual(mylist2) Then > ' do whatever > End If > > If that's not what you want to do, you need to be more specific about your > question. > > Pete On Thu, 28 May 2009 22:10:18 -0700, Nathan Sokalski
<njsokal***@hotmail.com> wrote: > I do have two objects, I just posted the declaration for one because both Please see my other replies in this thread.> objects have the same declaration (well, other than the name). Sorry > about > that. I want to compare them to the other List, basically meaning I want > to > make sure both Lists contain the same values. I think that in my case > Enumerable.SequenceEqual() will work, although it would be nice to have a > similar method that does not care about the order of the elements. > Thanks. > Also, and this question is a little bit less urgent, but is there a way You can't do it literally without a loop or a function, no. But, you can > to > do the same thing for an array of Lists, such as the following: > > Dim mylist(4) As New Collections.Generic.List(Of Integer) > Dim mylist2(4) As New Collections.Generic.List(Of Integer) > > In my case, I can obviously just call Enumerable.SequenceEqual() for each > index to create a Boolean expression, since my arrays are a small fixed > size, but in the case of larger or variable sized arrays, this would > require > a loop or function. Is there a way to do this for arrays without using a > loop or function? Thanks again. use the SequenceEquals() overload that allows you to provide an IEqualityComparer implementation. You'll still need that implementation (which means not only writing a new function, but actually writing a whole new class), but the loop would of course be encapsulated into the SequenceEquals() method. The IEqualityComparer implementation would in turn call the SequenceEquals() method on the List(Of Integer) elements passed to it. http://msdn.microsoft.com/en-us/library/bb342073.aspx for more details and a code example. Pete "Peter Duniho" <NpOeStPe***@nnowslpianmk.com> wrote in message The OP is, unfortunately, well-known for this...news:op.uuoayix68jd0ej@macbook-pro.local... > There is nothing about your question specific to C#, and by definition a > post on-topic in a VB newsgroup is off-topic in a C# newsgroup, and vice a > versa. > > Follow-ups modified appropriately. Please don't cross-post > inappropriately in the future. Almost every post he makes is inappropriately cross-posted... If the order of the elements is irrelevant , ie { 1 2 3} is considered
equal to { 1 3 2} (ie, you are using SETS, or some data collection where position in the list is irrelevant), order (sort) the two lists, than use SequenceEqual. return seqence1.OrderBy(i=>i).SequenceEqual(sequence2.OrderBy(i=>i)); Vanderghast, Access MVP Show quoteHide quote "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message news:OcxFNBB4JHA.1712@TK2MSFTNGP03.phx.gbl... >I have two objects that I need to compare the values of. The declaration of >the object is: > > Dim mylist As New Collections.Generic.List(Of Integer) > > How can I compare the values of the Integers in the List? Thanks. > -- > Nathan Sokalski > njsokal***@hotmail.com > http://www.nathansokalski.com/ >
getting error on record duplication
Visual Studio 2008 hogging memory partition numbers in four terms ListView - Caching Thumbnails? automating ie or webbrowser control Selecting dates in SQL Server Define irregularly shaped area in UserControl where controls may be dropped exporting dataset to Excel is slow Converting from Access 32bit 64bit |
|||||||||||||||||||||||