Resolved Option Strict vs Option Explicit

jcardana

Old to VB6, New to VB.NET
Joined
Oct 26, 2015
Messages
43
Location
Rio Rancho, NM
Programming Experience
Beginner
I'm watching THIS tutorial about Drag and Drop.
I'm using the following...
DragDrop Event:
Private Sub tvwCompDir_DragDrop(sender As Object, e As DragEventArgs) Handles tvwCompDir.DragDrop
    ' GET [SINGLE] FILE PATH FROM DROPPED FILE
    Dim PathFileNames() As String = e.Data.GetData(DataFormats.FileDrop)()
        
End Sub
I'm getting error BC3057: Option Strict On disallows late binding.

Turning OFF Option Strict eliminates the error, but contradicts what another tutorial states about it.
Unless I'm thinking about Option Explicit???
Having Option Explicit On, I get no errors.

Thanks for any clarifications on which Option (Strict or Explicit) I should be using,
Joe
 
Solution
I recommend Option Strict ON, Explicit ON, Infer ON. Set them as default in VS options: Visual Basic Defaults, Projects, Options Dialog Box - Visual Studio (Windows)
More about the options:
GetData returns type Object, convert it to string array
VB.NET:
Dim names = CType(e.Data.GetData(DataFormats.FileDrop), String())
...
I recommend Option Strict ON, Explicit ON, Infer ON. Set them as default in VS options: Visual Basic Defaults, Projects, Options Dialog Box - Visual Studio (Windows)
More about the options:
GetData returns type Object, convert it to string array
VB.NET:
Dim names = CType(e.Data.GetData(DataFormats.FileDrop), String())
 
Solution
Thank you for the links, knowledge AND solution! CType is still new to me ;)
 
There's never any justification for turning Option Explicit Off. That would enable you to use variables without explicitly declaring them. If you want to do that, VB.NET is the wrong language for you in the first place.

Option Strict On means that strict typing is enforced, i.e. if a particular type is expected somewhere then you need to provide that type, which may require a cast or conversion. It should ALWAYS be On at the project level and then turned Off only in those code files where it is specifically required, i.e. where you need to use late binding for some reason, e.g. Office Automation. Even then, you should use partial classes to keep the code in those files to an absolute minimum, so strict typing is still enforced everywhere it can be.
 

Latest posts

Back
Top