0

在 VB.net (2012) 我有以下代码:

For Each itm As ListViewItem In Me.lvCustomers
    If CDbl(itm.Tag) <> customer.Id Then Me.lvMerges.Items.Add(itm.Clone)
Next

使用Option Strict On我收到以下错误:

错误 2 重载解析失败,因为无法使用以下参数调用可访问的“添加”:“公共可覆盖函数添加(值作为 System.Windows.Forms.ListViewItem)作为 System.Windows.Forms.ListViewItem”:Option Strict On 不允许隐式转换从“对象”到“System.Windows.Forms.ListViewItem”。'Public Overridable Function Add(text As String) As System.Windows.Forms.ListViewItem':Option Strict On 不允许从 'Object' 到 'String' 的隐式转换。

我可以做一个 lvMerges.Items.Add(itm) ,它不会引发错误,但是我必须将它从 lvCustomers 列表视图中删除,我不想这样做。

有人可以解释我如何在不关闭Option Strict 的情况下使其正常工作吗?

目标是使用所有子项复制 ListviewItem。

4

1 回答 1

3

您在那里收到的错误告诉您,使用Option Strict Onon 您不能从to或进行隐式转换。因此,您需要改为进行显式转换。ObjectStringListViewItem

For Each itm As ListViewItem In Me.lvCustomers
    If CDbl(DirectCast(itm.Tag, String) <> customer.Id Then Me.lvMerges.Items.Add(DirectCast(itm.Clone, ListViewItem))
Next

那样有用吗?

于 2013-05-10T04:31:31.217 回答