Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im stuck trying to compare two list types. For example consider the below

VB
 public class Test
   private _number as integer

   public property Number as integer
     'get and set code here
   end property
 end class

public class Test2
 Private _number as integer

 public property Number as integer
   'get and set info here
 end property
end class


Now I create two lists one of each type

VB
dim List1 as list(of Test)
dim List2 as list(of Test2)


Im trying to test the lists to make sure they are of the same type before performing a union statement on them both.

in this example that they are both of the type Test

any good material / pointers that I can read upon please :)

SOLUTION FOUND
I found a way around it by taking the first item from each list and comparing their type like so.

dim x as object = list1(0)
dim y as object = List2(0)

if type.equals(x,y) then
 messagebox "they are the same"
end if
Posted
Updated 1-Mar-11 6:17am
v3
Comments
Henry Minute 1-Mar-11 11:32am    
How are you doing the test?
Simon_Whale 1-Mar-11 11:37am    
at the moment this

Dim t As Type = list1.GetType

but I can only see an indication of the class type from the assemblyQualifiedName as that gives

System.Collections.Generic.List`1[[WindowsApplication1.Test, WindowsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Henry Minute 1-Mar-11 11:40am    
Maybe a variation on Manas's answer
If TypeOf List1 = TypeOf List2 Then
' Blah blah
End If
Simon_Whale 1-Mar-11 11:47am    
you can't do that Henry as you have to

If Typeof List1 is x then
'Blah Blah
End if
Henry Minute 1-Mar-11 11:49am    
Bummer!

May be one what to handle it is to derive both Test1 and Test2 from base class Test. Then you can check if it is TypeOf GetType(Test) to check if they are of the same type.
 
Share this answer
 
Comments
Simon_Whale 1-Mar-11 12:13pm    
Thanks for the advise

Simon
If I understand correctly, you want to test if the list is of type Test or not. For that, you can do something like this:

VB
If TypeOf List1 Is Test Then
    Console.WriteLine("List1 is Test")
End If
 
Share this answer
 
Comments
Simon_Whale 1-Mar-11 12:13pm    
Thanks for the advise found a solution - added to my question above
You're not comparing "types". You're trying to do something with lists of the elements of completely unrelated types. Why?

Even though they are declared using identical code, they are completely different; so their instances are not assignment-compatible. If you want to store them in the same list (as you mentioned union), it can only be a polymorphous list if System.Object — closest common ancestor. In real life, you need just one type, or types having closer common base type.

Even though using these two types makes not sense, you could only compare two different instances of two different types per component (one Number equals to another Number). You can even create comparison operators between the two. You can override object.Equals and object.GetHashCode and than add operators == and !=. You can also define explicit or implicit conversion operators Test in Test2 and Test2 in Test.

In real life the two types make no sense, but I think you just want to understand how it all works.

Good luck,
—SA
 
Share this answer
 
Comments
Simon_Whale 1-Mar-11 12:11pm    
Thanks for your answer -SA.

the example above was a simplified example that I hoped would convey what I was attempting to do. In an insurance back office application re-write I need to union some results once they have left a rating engine.

But I needed to do this for different objects, so my silly grey matter suggested that I needed to add some error handling :(

I found a way around it by taking the first item from each list and comparing their type like so.

dim x as object = list1(0)
dim y as object = List2(0)

if type.equals(x,y) then
messagebox "they are the same"
end if
Sergey Alexandrovich Kryukov 1-Mar-11 12:20pm    
As I say, they are never equal (different type declaration means not equal in any case), unless you define otherwise. You can always use != == if you define operator.
Don't forget, if you use != == inside your definition it may cause stack overflow! so use ReferencesEqual inside, if you redefine anything about identity.

I must tell you, whole approach with list operation on different element types will lead you nowhere. If you explain your goals, you may get better advice.

--SA
A lot depends on how you define "equal" and what the actual format of the two classes are.

Assuming that Test and Test1 are different names for the same object, you would first compare the lengths of List1 and List2; if they are not the same, they are not equal. Then you would iterate through every object in List1 and compare every element with the corresponding element in the corresponding object in List2.

VB
Public Function AreTheyEqual( _
    ByVal L1 As List(Of Test), _
    ByVal L2 As List(Of Test1) _
) As Boolean

    If L1.Count <> L2.Count Then Return False

    For i As Integer = 0 To L1.Count - 1
        Dim T1 As Test = L1(i)
        Dim T2 As Test1 = L2(i)

        If T1.Property1 <> T2.Property1 Then Return False
        If T1.Property2 <> T2.Property2 Then Return False
        ....
    Next

    Return True
End Function


Things get a bit more challenging if the two classes are different: again, you would need to define what "equals" actually means.
 
Share this answer
 
v2
Comments
Simon_Whale 1-Mar-11 12:17pm    
Thanks Gregory

Test and Test1 would primarily be of the same type but one of results came from a class library - hope it makes sense.

But I wanted to make the sure that they were the same type so that I could add error handling into what is now becoming a generic routine.

like your solution but while reading yours I just finished putting the finishing touches to mine

I found a way around it by taking the first item from each list and comparing their type like so.

dim x as object = list1(0)
dim y as object = List2(0)

if type.equals(x,y) then
messagebox "they are the same"
end if

But thanks for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900