Option Strict On disallows late binding
Asked Answered
I

4

12

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?

Ihab answered 11/9, 2012 at 18:5 Comment(4)
What error do you want to fix?Simmer
What error? You don't say what error you received on which exactly line. Also, share the StrComp function. That's not a standard vb function.Bema
StrComp is a standard VB function - the call here is appropriate, so the error is 'probably' to do with the 'AreaCts' indexer assignment, but who knows.Carbamate
...or String.IsNullOrEmpty/Whitespace in this caseRibbing
F
17

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.

Foscalina answered 11/9, 2012 at 18:11 Comment(5)
@David W If you read the title you will know what error I am getting.Ihab
I got some code that only worked with strict = off, but I didn't want to disable it. Thx!Unconsidered
A better solution is to split your class out into parts that are Late Bound and Early Bound. Then you can create two partial classes and just use Option Strict Off for the Late Bound parts.Construe
@MattWilko That's a really good point. I'm surprised that I failed to mention that. I updated my answer to include that option. Thanks!Foscalina
Solution in my case: A DataGridView contains an property DataSource (an Object). To get any data from that Object (for example TableName) I added Partial to Class Form1, created a Class in a separate source file with the same Partial Class Form1 with 1 function, that takes the Datagridview as parameter and returns the Datagridview.DataSource.TableName. The first line in this new file is Option Strict OffVacuva
H
3

You need to make the BindingSource act as a strongly-typed data source. See the remarks in the documentation: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx

Haskel answered 11/9, 2012 at 18:23 Comment(0)
F
3

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
Foochow answered 20/12, 2020 at 22:15 Comment(1)
Using the TypeOf command solved my issue.Raffia
B
1

If you declared AreaCts without a type, ex:

Dim AreaCts as Array

Try

Dim AreaCts() as Double

This fixed my late binding error.

Bronder answered 13/6, 2018 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.