【问题标题】:Option Strict On disallows late binding in VB6 migrated codeOption Strict On 禁止在 VB6 迁移代码中进行后期绑定
【发布时间】:2018-10-22 14:40:44
【问题描述】:

我已将 VB6 控件迁移到 Vb.Net,当我启用了 option strict 时,我收到“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 不允许后期绑定,我需要帮助修改代码以使用它。

【问题讨论】:

  • 你是如何在VB.NET中定义m_colRows的?
  • @steve :- 我这样定义 :- Private m_colRows As Collection
  • 如果 Item 返回一个对象,您需要将其转换为正确的类。

标签: vb.net vb6-migration


【解决方案1】:

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

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

【讨论】:

  • 非常感谢超级猫。但上面的例子是在使用前将其中保存的引用转换为控制引用。请给我一个将 ColRows 更改为通用集合的示例(例如 List(Of Control())
  • @vandy:没有看到您是如何声明或使用m_ColRows 的,我无法确定使用其他代码需要什么。声明应该类似于Dim m_ColRows as New List(Of Control()),但很可能还需要其他更改。例如,自从我使用 VB6 风格的集合以来已经有很长时间了,但我认为它的项目编号为 1..n 而不是 0..n-1(就像 List(of Collection()) 的情况一样,这可能 [取决于关于集合还有什么其他操作]最容易通过添加一个虚拟元素零来处理......
  • ...并且从不使用它,或者通过代码访问.Item(m_lngCurRow-1),但最好通过更改代码来处理,以便 m_lngCurRow 从 0 变为 n-1。但是,如果不查看您的其余代码,则无法确定哪种方法最适合您的需求。
  • 如果 VB6 代码如下所示,您对 VB.Net 有何建议? Private m_colBookmarks As Collection Private Property Get BookmarkCompID(ByVal strBookmarkNum As String) As String On Error Resume Next BookmarkCompID = m_colBookmarks(strBookmarkNum)(COL_ERR_BKMARK_COMP_ID) End Property
  • @vandy:您是否只在集合中使用 1..N 范围内的数字 ID?我建议考虑用 List(Of Control()) 或 [List(Of List(Of Control)) 替换它,如果一致,可能会替换您正在使用的任何类型的控件),但需要注意的是索引需要调整为从 0 开始。 .N-1 而不是 1..N。你必须手动找到所有需要调整的地方,如果你错过任何地方,代码就会有问题,但这种方法应该会产生最好的代码。
【解决方案2】:

消息是正确的。 Option Strict 确实不允许后期绑定。

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

您可以选择后期绑定或选项严格,但不能同时拥有。

你唯一的选择是

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

【讨论】:

    猜你喜欢
    • 2019-06-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多