如何确定ArrayList中的每个对象是什么?

时间:2014-10-21 00:17:32

标签: asp.net vb.net arraylist

我有一个包含Foos和Bars的ArrayList 如何迭代这个ArrayList来确定每个项目是Foo还是Bar? 我认为它会像我的下面尝试一样简单,但我得到一个警告,Foo和Bar是类型,不能用作表达式。

Dim myArrayList As New ArrayList
Dim foo As New Foo
Dim bar As New Bar

myArrayList.add(foo)
myArrayList.add(bar)

For counter As Integer = 0 To myArrayList.Count
    If myArrayList.Item(counter).GetType() = Foo Then
        Response.Write("Item " & counter & " is a Foo")
    Else
        Response.Write("Item " & counter & " is a Bar")
    End If
Next

1 个答案:

答案 0 :(得分:1)

试试这个

If TypeOf myArrayList.Item(counter) Is Foo Then
    Response.Write("Item " & counter & " is a Foo")
Else
    Response.Write("Item " & counter & " is a Bar")
End If