Question [VB2017] Late Binding Error - Option Strict on

Slabs1960

Member
Joined
Mar 19, 2017
Messages
19
Programming Experience
10+
When compiling, with Option Strict on, I get a Late Binding Error on the following code. I have looked for an answer and tried several options to try and fix it, with no success.

VB.NET:
Dim strParam1 As String()
            Dim arrayParameters As ArrayList = Nothing
            Dim strParameterText As String = ""
                strParameterText = DGVScadaEquipmentGeneration.Rows(r).Cells("Param").Value.ToString
                Dim strSeparators() As Char = {";"c}
                strWords = strParameterText.Split(strSeparators)
                Dim Names As String() = strParameterText.Split(strSeparators)
                For Each n As String In Names
                    arrayParameters.Add(n)
                Next
                'Get Parameters
                strSeparators = {"="c, ","c}
                [HIGHLIGHT]strParam1 = arrayParameters(0).Split(strSeparators)[/HIGHLIGHT] <-- Late Binding error
                lblParameter1.Text = strParam1(0) & "="
                For Each n As String In strParam1
                    If n <> "plc" Then
                        lboxParameter1.Items.Add(n)
                    End If
                Next


Code works with Option Strict off. How do I fix this?
 
The issue is that the Item property of an ArrayList is type Object so it has no Split method. If you were using .NET 1.x then the way to fix it would be to cast that item as type String so that you can access the members of the String type:
strParam1 = CStr(arrayParameters(0)).Split(strSeparators)

For anyone using .NET 2.0 or later though - which means anyone writing code from 2005 - the solution is to not use an ArrayList in the first place. You should be using a List(Of T) where you would previously have used an ArrayList, because you get to specify what type T is and then the list will only support items of that type. In your case, you'd use a List(Of String) and then the Item property is type String and there's no cast required.

That said, what's the point of using any list in that case? What's the point of copying the elements from your Names array into a list at all when you can just use Names later on where you're using that list? Get rid of the list altogether and change that offending line to this:
strParam1 = Names(0).Split(strSeparators)
 
Thanks. List(Of String) fixed it.

That said, what's the point of using any list in that case? What's the point of copying the elements from your Names array into a list at all when you can just use Names later on where you're using that list?

The string is long, and has several sections to it. I am grouping portions of the string into separate lists, to be used later.

VB.NET:
strParameterText = DGVScadaEquipmentGeneration.Rows(r).Cells("Param").Value.ToString
                Dim strSeparators() As Char = {";"c}
                strWords = strParameterText.Split(strSeparators)
                Dim Names As String() = strParameterText.Split(strSeparators)
            For Each n As String In Names
                arrayParameters.Add(n)
            Next
            'Get Parameters
            strSeparators = {"="c, ","c}
                strParam1 = arrayParameters(0).Split(strSeparators)
                lblParameter1.Text = strParam1(0) & "="
                For Each n As String In strParam1
                    If n <> "plc" Then
                        lboxParameter1.Items.Add(n)
                    End If
                Next
                strSeparators = {"="c, ","c}
                strParam2 = arrayParameters(1).Split(strSeparators)
                lblParameter2.Text = strParam2(0) & "="
                For Each n As String In strParam2
                    If n <> "alarm" Then
                        If n <> "analog" Then
                            lboxParameter2.Items.Add(n)
                        End If
                    End If
                Next
                Try
                    strParam3 = arrayParameters(2).Split(strSeparators)
                    lblParameter3.Text = strParam3(0) & "="
                    lboxParameter3.Show()
                    For Each n As String In strParam3
                        If n <> "alarm" Then
                            lboxParameter3.Items.Add(n)
                        End If
                    Next
                Catch
                    lboxParameter3.Hide()
                End Try
 
The string is long, and has several sections to it. I am grouping portions of the string into separate lists, to be used later.

That isn't justification for using a collection though. Once you have create and populated the collection, you never change it, so why not just use the array that contains exactly the same data?
 

Latest posts

Back
Top