With creating new project VB.NET IDE automatically set Option Strict to Off by default.
So I often put Option Strict On at top of my files as a good programming practice and for that files the rule of Option Strict is applied.
Now (for some time), when creating a new project I set Option Strict to On under project's properties/compile so that is applied to whole project without need to specify that on every file.
But Now I use one foreign program under my project where data types are not fully known for me and I would like to keep Option Strict for all files in project except for that one.
I try with Option Strict Off at top of that file but that seem not enough. There are listed much of typical errors under my IDE/Errors tab.
How to turn off Option Strict for just one file under project which have Option Strict turned On under Compiler properties of project?
Public Class ApplicationBar
Inherits NativeWindow
' Debugging Mode On/Off
#Const DEBUG_MODE = False
' SetWindowLong selectors
Const GWL_WNDPROC = -4&
' Windows messages
Const WM_ACTIVATE = &H6
Const WM_GETMINMAXINFO = &H24
Const WM_ENTERSIZEMOVE = &H231
Const WM_EXITSIZEMOVE = &H232
Const WM_MOVING = &H216
Const WM_NCHITTEST = &H84
Const WM_NCMOUSEMOVE = &HA0
Const WM_SIZING = &H214
Const WM_TIMER = &H113
Const WM_WINDOWPOSCHANGED = &H47
' WM_SIZING Selectors
Const WMSZ_LEFT = 1
Const WMSZ_RIGHT = 2
Const WMSZ_TOP = 3
Const WMSZ_TOPLEFT = 4
Const WMSZ_TOPRIGHT = 5
Const WMSZ_BOTTOM = 6
Const WMSZ_BOTTOMLEFT = 7
Const WMSZ_BOTTOMRIGHT = 8
' Appbar messages
Const ABM_NEW = &H0
Const ABM_REMOVE = &H1
Const ABM_QUERYPOS = &H2
Const ABM_SETPOS = &H3
Const ABM_GETSTATE = &H4
Const ABM_GETTASKBARPOS = &H5
Const ABM_ACTIVATE = &H6
Const ABM_GETAUTOHIDEBAR = &H7
Const ABM_SETAUTOHIDEBAR = &H8
Const ABM_WINDOWPOSCHANGED = &H9
Const ABM_SETSTATE = &HA
' Appbar edges
Const ABE_LEFT = 0
Const ABE_TOP = 1
Const ABE_RIGHT = 2
Const ABE_BOTTOM = 3
Const ABE_UNKNOWN = 4
Const ABE_FLOAT = 5
'Appbar allowed floats
Const ABF_ALLOWLEFT = 1
Const ABF_ALLOWRIGHT = 2
Const ABF_ALLOWTOP = 4
Const ABF_ALLOWBOTTOM = 8
Const ABF_ALLOWFLOAT = 16
' The ABN_* constants are defined here as follows:
'Const ABN_STATECHANGE = &H0
Const ABN_POSCHANGED = &H1
Const ABN_FULLSCREENAPP = &H2
Const ABN_WINDOWARRANGE = &H3
' GetKeyState and GetAsyncKeyState Selectors
Const VK_LBUTTON = &H1
Const VK_RBUTTON = &H2
Const VK_CONTROL = &H11
' MessageBox Selectors
Const MB_OK = &H0&
Const MB_ICONINFORMATION = &H40&
' ModifyStyle Selectors
Const GWL_STYLE = (-16)
Const GWL_EXSTYLE = (-20)
Const WS_CAPTION = &HC00000
Const WS_SYSMENU = &H80000
Const WS_EX_APPWINDOW = &H40000
Const WS_BORDER = &H800000
' SetWindowPos Selectors
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOZORDER = &H4
Const SWP_NOACTIVATE = &H10
Const SWP_DRAWFRAME = &H20
Const HWND_NOTOPMOST = -2
Const HWND_TOPMOST = -1
Const HWND_BOTTOM = 1
' ShowWindow Selectors
Const SW_HIDE = 0
Const SW_SHOW = 5
' WM_ACTIVATE Selectors
Const WA_INACTIVE = 0
'Custom Defaults
Private Const AB_DEF_SIZE_INC As Integer = 1
Private Const AB_DEF_DOCK_SIZE As Integer = 32
' We need a timer to determine when the AppBar should be re-hidden
Const AUTO_HIDE_TIMER_ID = 100
Const SLIDE_DEF_TIMER_INTERVAL = 400 ' milliseconds
' Subclassing function default result
Const INHERIT_DEFAULT_CALLBACK = -1
All those variables are underlined with green as NOT explicitely declared and are treated as errors under Option Strict On.
What you describe should work fine. It is very common to have Option Strict On
for the whole project, but then just override it to Option Strict Off
on certain files. Therefore your problem is most likely something else. For instance, perhaps your problem is not with Option Strict
, but rather with one of the other options, such as Option Explicit
or Option Infer
.
Setting Option Strict Off at the top of the file will work correctly. We do exactly the same as you (add Option Strict On at the top of all code files) and also require it to be set on in new projects.
However, there are occasionally scenarios where Option Strict Off is required (certain activities with Crystal Reports, for example). In these cases, we isolate just the code that requires Option Strict Off, move it to its own file, and add the directive to the top.
** UPDATE based on new code:
The solution to the problem is to add the following to the top of the file in addition to Option Strict Off
:
Option Infer On
This will allow VB to automatically determine and assign appropriate data types to each of the objects.
HOWEVER: if you are using these values in Windows API calls, you must ensure the types that VB picks for you are correct. You can do this by hovering the cursor over the declaration: VB will show the full type declaration.
If this file originated in another VB project, then the originating project almost certainly had Option Infer On set in the project properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With