anyone knows what is option strict on is used for? or it means wat?

Dani AI

Generated

Quick clarification and a practical guide.

Good attempt — the behavior you described (forcing declarations) is actually controlled by Option Explicit. is closer: Option Strict changes how the compiler handles conversions and late binding. The short, practical summary: Option Strict On makes VB perform stricter compile-time type checks so many unsafe or implicit operations fail to compile and must be made explicit.

What Option Strict On enforces:

  • No implicit narrowing conversions (you must convert explicitly when precision or range might be lost).
  • No late binding (calling members on variables typed as Object requires a cast to a known type or interface).
  • No implicit conversions from Object to specific types without a cast.

Examples you can try locally:

Option Strict On

Dim s As String = "123"
Dim i As Integer = s                     ' Error: implicit conversion disallowed
Dim i2 As Integer = Convert.ToInt32(s)   ' OK
Dim i3 As Integer
If Integer.TryParse(s, i3) Then          ' Robust conversion pattern
    ' OK
End If

Dim obj As Object = CreateComObject()
Dim r = obj.SomeMethod()                 ' Error: late binding
Dim r2 = CType(obj, IMyInterface).SomeMethod()   ' OK if obj implements IMyInterface

How to enable it: put Option Strict On at the top of a source file, or set it project-wide in Project Properties -> Compile -> Option Strict. When converting a legacy project, enable it module-by-module, fix one class at a time, and prefer safe conversion patterns like TryParse, CType/DirectCast/TryCast, or explicit Convert.* calls.

Official reference material:

anyone knows what is option strict on is used for? or it means wat?

With VB, you can implicitly declare variables, which means that you do not have to declare your variables prior to using them. This statement forces you declare all variables prior to using them. If you do not have this statement, vb has to guess at what type of variable it is and you can have misspellings leading to a lot of variables which will make your code bomb.

Chester

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.