列表类型比较-需要建议 [英] list type comparison - advice required
本文介绍了列表类型比较-需要建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直想尝试比较两种列表类型.例如,考虑以下
Im stuck trying to compare two list types. For example consider the below
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
dim List1 as list(of Test)
dim List2 as list(of Test2)
我试图在对两个列表执行联合声明之前测试这些列表,以确保它们具有相同的类型.
在此示例中,它们都是 Test
类型
我可以阅读的任何好的材料/指针:)
解决方案
通过从每个列表中获取第一项并像这样比较它们的类型,我找到了解决方法.
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
推荐答案
可能要解决的一个问题是从基类Test
派生Test1
和Test2
.然后,您可以检查是否为TypeOfGetType(Test)
来检查它们是否属于同一类型.
May be one what to handle it is to derive bothTest1
andTest2
from base classTest
. Then you can check if it isTypeOfGetType(Test)
to check if they are of the same type.
如果我理解正确,您想测试一下list的类型是否为Test.为此,您可以执行以下操作:
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:
If TypeOf List1 Is Test Then
Console.WriteLine("List1 is Test")
End If
您不是比较类型".您正在尝试使用完全不相关类型的元素列表进行操作.为什么?
即使使用相同的代码声明它们,也完全不同.因此它们的实例不兼容分配.如果要将它们存储在同一列表中(如您所提到的并集),则只有System.Object
—最接近的共同祖先,它才可以是多态列表.在现实生活中,您只需要一种类型,或者具有更接近的通用基本类型的类型.
即使使用这两种类型没有意义,您也只能比较每个组件两种不同类型的两种不同实例(一个Number
等于另一个Number
).您甚至可以在两者之间创建比较运算符.您可以覆盖object.Equals
和object.GetHashCode
,然后添加运算符==
和!=
.您还可以在Test2
中定义显式或隐式转换运算符Test
,在Test
中定义Test2
.
在现实生活中,这两种类型毫无意义,但我认为您只想了解它们的工作原理.
祝你好运,
—SA
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 ifSystem.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 (oneNumber
equals to anotherNumber
). You can even create comparison operators between the two. You can overrideobject.Equals
andobject.GetHashCode
and than add operators==
and!=
. You can also define explicit or implicit conversion operatorsTest
inTest2
andTest2
inTest
.
In real life the two types make no sense, but I think you just want to understand how it all works.
Good luck,
—SA
这篇关于列表类型比较-需要建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文