Search code examples
vb.netvb6-migration

Option Strict On disallows late binding in VB6 migrated code


I have migrated a VB6 control to Vb.Net and when I had option strict on, I am getting "Option Strict On disallows late binding" error. Below I have mentioned VB6 code as well as migrated code in detail.

VB6 Code:-

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

Below is the Migrated code(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 disallows late binding and I need help on modifying the code to work with it on.


Solution

  • The VB6 Collection type holds references of type Object. If you wish to use the .Text method on members thereof, you will either have to change ColRows into a generic collection (e.g. a List(Of Control()) or else convert the references held in it into Control references before use (e.g.

    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
    

    Without seeing more of your code, I can't tell which approach would be easier and/or would yield better results. I would guess that using a generic collection would likely yield cleaner code, but the VB6-style Collection type supports some constructs that the generic ones generally don't, including the ability to modify the collection during enumeration, which can sometimes make porting tricky.