vandy
vandy

Reputation: 63

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.

Upvotes: 1

Views: 448

Answers (2)

Terry Carmen
Terry Carmen

Reputation: 3876

The message is correct. Option Strict does disallow late binding.

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

You can pick late binding or option strict but you can't have both.

Your only options are to

  • Turn off late binding
  • Change your code so it doesn't use late binding
  • Turn off "option strict"

Upvotes: 1

supercat
supercat

Reputation: 81115

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.

Upvotes: 3

Related Questions