Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Option Strict On disallows late binding

Can someone help me fix this error?

Option Strict On disallows late binding

Here's the code that's causing the error:

Dim SF6StdData As BindingSource = New BindingSource() 
' ...
If StrComp(SF6StdData.Current("O2AreaCts").ToString, "") = 0 Then
    AreaCts(3) = 0
Else
    AreaCts(3) = Convert.ToDouble(SF6StdData.Current("O2AreaCts").ToString)
End If

I need to rewrite the code so it will not have any errors. I know I could fix this by setting Option Strict to Off in the project properties, but I really don't want to do that. Is there some other way?

like image 686
mike Avatar asked Sep 11 '12 18:09

mike


People also ask

What is Option Strict On Uipath?

Help Activities. uiautomation.

How do I turn strict on in Visual Basic?

When you create a project, the Option Strict setting on the Compile tab is set to the Option Strict setting in the Options dialog box. To set Option Strict in this dialog box, on the Tools menu, click Options. In the Options dialog box, expand Projects and Solutions, and then click VB Defaults.


2 Answers

Late binding is not allowed when Option Strict is on. If you need to perform late binding, the only options are either to use reflection or to shut off Option Strict. The one saving grace, however, is that you don't have to shut off Option Strict for the whole project. You can leave it on for the project and then just add the line Option Strict Off at the top of any code files where you need to perform late binding. It's not a great solution, but it's better than affecting the whole project.

Also, since the Option Strict placed at the top of a file applies just to that file, it doesn't even have to apply to an entire class. If you split your class into multiple Partial Class files, then you could have Option Strict set differently for each of those files. For instance, if you put most of your class in a file with Options Strict On, and then just put one method in a Partial Class in a separate file with Option Strict Off, then only that one method would be compiled loosely. The rest of the class would be compiled using the strict rules.

like image 89
Steven Doggart Avatar answered Oct 14 '22 21:10

Steven Doggart


This is an old post, but I struggled with the error "Option Strict On disallows late binding". Maybe another answer will help someone else. The problem may be coming when you try to convert the data in your SF6StdData bindingsource to a string. You can probably solve the problem by defining a local variable with the desired type, and then using Ctype to extract the data into the correct type. Here's an example of how I solved a similar problem.

This code gave me the late-binding error:

    Friend Function CountNumCheckedInGroupbox(ByVal gbox As GroupBox, ByRef nameschecked() As String) As Integer
        Dim numchecked As Integer = 0
        For Each ctrl In gbox.Controls
            If TypeOf ctrl Is CheckBox Then                
                If ctrl.Checked = True Then
                    nameschecked(numchecked) = ctrl.Text
                    numchecked += 1
                End If
            End If
        Next
        Return numchecked
    End Function

The late binding error occurred where I referenced "ctrl.Checked" and "ctrl.Text"

Instead of referencing "ctrl" directly, I defined a variable cbox that is typed as a Checkbox. Then I extracted the information from "ctrl" into cbox. Now the code does not show late-binding errors:

    Friend Function CountNumCheckedInGroupbox(ByVal gbox As GroupBox, ByRef nameschecked() As String) As Integer
        Dim numchecked As Integer = 0
        Dim cbox As CheckBox
        For Each ctrl In gbox.Controls
        If TypeOf ctrl Is CheckBox Then
                cbox = CType(ctrl, CheckBox)
                If cbox.Checked = True Then
                    nameschecked(numchecked) = cbox.Text
                    numchecked += 1
                End If
            End If
        Next
        Return numchecked
    End Function
like image 26
Robert Cody Avatar answered Oct 14 '22 23:10

Robert Cody