What is the need of Option Strict On in VB.NET ?


The advantage of using OPTION STRICT ON is that it makes the code strongly typed. So it allows implicit data conversions for widening conversions only. Thus it results in no data loss. Also, conversion between numeric types and strings are also avoided

Use of Option Strict On


When programming in VB.NET, you should always do OPTION STRICT ON.

By default, VB.NET has OPTION STRICT OFF. So the program will do implicit type conversions and late bindings which leads to performance problems and run-time errors. Also, it will be difficult to understand the code later on. So it is a sub standard programming practice.

The advantage of using OPTION STRICT ON is that it makes the code strongly typed. So it allows implicit data conversions for widening conversions only. Thus it results in no data loss. Also, conversion between numeric types and strings are also avoided

Widening Conversions


Widening conversions means...

Byte can be converted to Byte, Short, Integer, Long, Decimal, Single, Double

Short can be converted to Short, Integer, Long, Decimal, Single, Double

Integer can be converted to Integer, Long, Decimal, Single, Double

Char can be converted to Char, String

and so on..
but integer cannot be converted to byte. (here, not from right to left)


How to set the option strict on in visual studio?



Go to Tools -> Options -> Projects & Solutions -> VB Defaults -> Set Option Strict On


Comments

Author: Preethu30 May 2012 Member Level: Silver   Points : 0

It speeds up the code also.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: