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

时间:2018-10-22 14:40:44

标签: vb.net vb6-migration

我已经将VB6控件迁移到Vb.Net,并且当我严格执行选项时,出现“ Option Strict On不允许后期绑定”错误。下面,我详细提到了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不允许后期绑定,我需要修改代码以对其进行处理。

2 个答案:

答案 0 :(得分:3)

VB6 Collection类型保存着Object类型的引用。如果您希望在其成员上使用.Text方法,则必须将ColRows更改为通用集合(例如List(Of Control()),或者将其中保存的引用转换为{{ 1}}使用之前的引用(例如

Control

在没有看到更多代码的情况下,我无法说出哪种方法更容易和/或产生更好的结果。我猜想使用通用集合可能会产生更简洁的代码,但是VB6风格的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 类型支持通用结构通常不支持的某些构造,包括在枚举期间修改集合的能力,有时使移植变得棘手。

答案 1 :(得分:1)

该消息是正确的。 Option Strict 不会禁止后期绑定。

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

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

您唯一的选择是

  • 关闭后期绑定
  • 更改代码,使其不使用后期绑定
  • 关闭“严格选项”