In VB.net (2012) I have the following code:

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

With Option Strict On I get the following error:

Error 2 Overload resolution failed because no accessible 'Add' can be called with these arguments: 'Public Overridable Function Add(value As System.Windows.Forms.ListViewItem) As System.Windows.Forms.ListViewItem': Option Strict On disallows implicit conversions from 'Object' to 'System.Windows.Forms.ListViewItem'. 'Public Overridable Function Add(text As String) As System.Windows.Forms.ListViewItem': Option Strict On disallows implicit conversions from 'Object' to 'String'.

I can do an lvMerges.Items.Add(itm) , which does not throw an error, but then I have to remove it from the lvCustomers listview, which I do not want to do.

Can someone explain how I can make this work properly without turning off Option Strict?

Goal is to copy ListviewItem with all SubItems.

有帮助吗?

解决方案

The error you've received there tells you that with Option Strict On on you can't do implicit casting from Object to String or ListViewItem. So you need to do explicit casting instead.

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

Does that work?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top