Option Strict On 不允许在 VB6 迁移代码中进行后期绑定

Option Strict On disallows late binding in VB6 migrated code

我已将 VB6 控件迁移到 Vb.Net,当我启用严格选项时,出现 "Option Strict On disallows late binding" 错误。下面我已经详细提到了VB6代码以及迁移代码。

VB6代码:-

Private m_colRows As Collection    'Represents the rows in a table
Private m_lngCurrCol As Long 'Control variable for Col Property
Private m_lngCurrRow As Long 'Control variable for Row Property

Public Property Let CellText(ByVal strText As String)
     m_colRows(m_lngCurrRow)(m_lngCurrCol).Text = strText
End Property
Public Property Get CellText() As String
   CellText = m_colRows(m_lngCurrRow)(m_lngCurrCol).Text
End Property

下面是迁移后的代码(Vb.Net)

Public Property CellText() As String
    Get
        CellText = m_colRows.Item(m_lngCurrRow)(m_lngCurrCol).Text
    End Get
    Set(ByVal Value As String)
        m_colRows.Item(m_lngCurrRow)(m_lngCurrCol).Text = Value
    End Set
End Property

Option Strict On 不允许后期绑定,我需要帮助修改代码以使用它。

消息正确。 Option Strict 是否 禁止后期绑定。

https://docs.microsoft.com/en-us/dotnet/visual-basic/misc/bc30574

您可以选择延迟绑定或选项严格,但不能同时选择两者。

你唯一的选择是

  • 关闭后期绑定
  • 更改您的代码,使其不使用后期绑定
  • 关闭"option strict"

VB6 Collection 类型包含 Object 类型的引用。如果您希望对其成员使用 .Text 方法,则必须将 ColRows 更改为通用集合(例如 List(Of Control()) 或将其中包含的引用转换为 Control 使用前参考(例如

Public Property CellText() As String
    Get
        CellText = CType(m_colRows.Item(m_lngCurrRow), Control())(m_lngCurrCol).Text
    End Get
    Set(ByVal Value As String)
        CellText = CType(m_colRows.Item(m_lngCurrRow), Control())(m_lngCurrCol).Text = Value
    End Set
End Property

没有看到您的更多代码,我无法判断哪种方法更容易 and/or 会产生更好的结果。我猜想使用泛型集合可能会产生更清晰的代码,但是 VB6 风格的 Collection 类型支持一些泛型集合通常不支持的结构,包括在枚举期间修改集合的能力,这有时可以让移植变得棘手。