none
option strict on - is better? RRS feed

  • Question

  • HI Guys,

    what do you thinkg about comment below? original article url is also suggested to read.

    I didnt get why my code will be faster if i use it on. anyone has good knowledge about it?

     

    Although Visual Basic . NET   allows you to perform implicit type conversions and late binding, you should avoid these practices. Implicit type conversions and late binding may lead to severe performance problems, runtime errors, code that is difficult to read and maintain, and sub-standard programming practices.  Many VB. NET   professional programmers believe that leaving OPTION STRICT OFF by default was perhaps Microsoft's worst decision in the VB. NET implementation. Use OPTION STRICT ON.

     

    source : http://www.getdotnetcode.com/gdncstore/free/Articles/OPTION%20STRICT%20ON.htm

     

    Monday, June 28, 2010 7:43 PM

Answers

  •  

    Hi emil_tr and Frank and everyone else,

    I have explained these in an earlier thread in great detail but I'm starting to get too many threads in "My Threads". :-(

    It is becoming difficult for me to find specific threads. I might start a new forum profile or index my threads on a website in some way.

     

    Anyway:

    OPTION STRICT ON:

    This will ensure that you convert the TYPEs of variables to the correct type so you do not get unexpected results.

    If you get squiggly lines under bits of your code, hover over them and see what corrections are offered by hovering on the

    red circle with the white exclamation mark, then click the down arrow. E.G:

     

     

    The offered suggestion is to put the number 234 into a string with CStr - Convert to STRing

    you could of course write any of the following instead.>>

    Dim aString1 As String = "234"
    Dim aString2 As String = Convert.ToString(234)
    Dim aString3 As String = CStr(234)
    Dim aString4 As String = 234.ToString

    If trying to convert the string representation of a number back to an INTEGER or some other type there are a few methods you could use.

     I would recommend the TryParse method.

    Use the TYPE followed by a dot then use TryParse as in.>>

    Integer.TryParse

    >>

    Dim aString1 As String = "234"
    Dim value As Integer
    Integer.TryParse(aString1, value)

     


    Regards, John : Trying hard for another goal into the .Net!!
    • Marked as answer by emil_tr Tuesday, June 29, 2010 6:10 PM
    Tuesday, June 29, 2010 2:01 AM
  • OPTION EXPLICIT

     

    Specifically OPTION EXPLICIT ON requires all variables to be declared with; DIM, STATIC, REDIM, PRIVATE or PUBLIC,

    in other words it enforces EXPLICIT declaration which enables you to see the scope of a variable better too.

    :-)

    If it was OFF this is allowed where variable is an OBJECT.

    For proof PASTE the following code into Vb.Net and hover over variable

    Option Strict Off
    Option Explicit Off
    Option Infer Off
    
    Public Class Form1
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
     variable = 234
    
     End Sub
    End Class

     


    Regards, John : Trying hard for another goal into the .Net!!
    Tuesday, June 29, 2010 2:22 AM
  • OPTION INFER

     

    When ON    Vb.Net infers the TYPE of what follows to the variable.

    In other words;

    Dim variable = 234

    variable would be inferred to be an INTEGER

     

    Dim variable = "234"

    variable would be inferred to be a STRING

     

    In other words Vb.Net is doing its best it can with the item following the assignment operator which is the equals sign.

    However as this may also produce unwanted results in your code it is better to leave OPTION INFER OFF

    and you can then also more clearly see what TYPE a variable is like this.>>

    Dim variable As Integer = 234

     

    Summary of recommendations:

    OPTION STRICT ON

    OPTION EXPLICIT ON

    OPTION INFER OFF

     

    1) Go to the last item in the PROJECT menu like "WindowsApplication1 Properties..."

    2) Click the COMPILE tab on the left of the window that appears and change the settings in there for your project. :-)

     


    Regards, John : Trying hard for another goal into the .Net!!
    • Marked as answer by emil_tr Tuesday, June 29, 2010 6:11 PM
    Tuesday, June 29, 2010 2:32 AM

All replies

  • Option Strict ON... always, at all times. Change it in the Tools menu in VS2008 so it is ON by default for all projects

    that article sums it up pretty well. No need to say more :)

    • Proposed as answer by JohnWein Monday, June 28, 2010 9:02 PM
    Monday, June 28, 2010 8:45 PM
  • Hi emil_tr,

    Recommended:

    Option Strict On

    Option Explicit On

    Option Infer Off

    If you run into any problems with these settings in your code, please start a new question. :-)    ;-)


    Regards, John : Trying hard for another goal into the .Net!!
    Monday, June 28, 2010 11:35 PM
  • John and/or Joe:

    WHY should Option Strict be on? WHY should Explicit be on? WHY?

    What do these mean? What are the conseqences of not doing so?

    :)

    My point here is this: There has never been a thread which is more inviting of a really GOOD explanation (hopefully others will chime in also) that explains these things and goes into depth beyond 'just do it this way'. How about this becomes a thread that we can all later reference when discussing it.

    @ John -> You're a heck of a documenter by nature.

    Go with it my friend! :-D

    Tuesday, June 29, 2010 12:59 AM
  •  

    Hi emil_tr and Frank and everyone else,

    I have explained these in an earlier thread in great detail but I'm starting to get too many threads in "My Threads". :-(

    It is becoming difficult for me to find specific threads. I might start a new forum profile or index my threads on a website in some way.

     

    Anyway:

    OPTION STRICT ON:

    This will ensure that you convert the TYPEs of variables to the correct type so you do not get unexpected results.

    If you get squiggly lines under bits of your code, hover over them and see what corrections are offered by hovering on the

    red circle with the white exclamation mark, then click the down arrow. E.G:

     

     

    The offered suggestion is to put the number 234 into a string with CStr - Convert to STRing

    you could of course write any of the following instead.>>

    Dim aString1 As String = "234"
    Dim aString2 As String = Convert.ToString(234)
    Dim aString3 As String = CStr(234)
    Dim aString4 As String = 234.ToString

    If trying to convert the string representation of a number back to an INTEGER or some other type there are a few methods you could use.

     I would recommend the TryParse method.

    Use the TYPE followed by a dot then use TryParse as in.>>

    Integer.TryParse

    >>

    Dim aString1 As String = "234"
    Dim value As Integer
    Integer.TryParse(aString1, value)

     


    Regards, John : Trying hard for another goal into the .Net!!
    • Marked as answer by emil_tr Tuesday, June 29, 2010 6:10 PM
    Tuesday, June 29, 2010 2:01 AM
  • OPTION EXPLICIT

     

    Specifically OPTION EXPLICIT ON requires all variables to be declared with; DIM, STATIC, REDIM, PRIVATE or PUBLIC,

    in other words it enforces EXPLICIT declaration which enables you to see the scope of a variable better too.

    :-)

    If it was OFF this is allowed where variable is an OBJECT.

    For proof PASTE the following code into Vb.Net and hover over variable

    Option Strict Off
    Option Explicit Off
    Option Infer Off
    
    Public Class Form1
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
     variable = 234
    
     End Sub
    End Class

     


    Regards, John : Trying hard for another goal into the .Net!!
    Tuesday, June 29, 2010 2:22 AM
  • OPTION INFER

     

    When ON    Vb.Net infers the TYPE of what follows to the variable.

    In other words;

    Dim variable = 234

    variable would be inferred to be an INTEGER

     

    Dim variable = "234"

    variable would be inferred to be a STRING

     

    In other words Vb.Net is doing its best it can with the item following the assignment operator which is the equals sign.

    However as this may also produce unwanted results in your code it is better to leave OPTION INFER OFF

    and you can then also more clearly see what TYPE a variable is like this.>>

    Dim variable As Integer = 234

     

    Summary of recommendations:

    OPTION STRICT ON

    OPTION EXPLICIT ON

    OPTION INFER OFF

     

    1) Go to the last item in the PROJECT menu like "WindowsApplication1 Properties..."

    2) Click the COMPILE tab on the left of the window that appears and change the settings in there for your project. :-)

     


    Regards, John : Trying hard for another goal into the .Net!!
    • Marked as answer by emil_tr Tuesday, June 29, 2010 6:11 PM
    Tuesday, June 29, 2010 2:32 AM
  • I'm agree with Option Explicit On.

    I have some problem with ruby about this.

    Ex. 

    Index = 5

    Console.Writeline( ArrayString(Indez) )

    Without warning you already plan a bomb in your program.

     

    But I disagree with Strict ON and Infer OFF.

    Infer only work under method (like sub or function).

    Noway they can curse any trouble.

     

    I'm anti Strict ON.I haven't any trouble without it.

    Strict option doesn't make program run faster

    but make it lower chance of code error from dev.

    Tuesday, June 29, 2010 6:32 AM
  • Bravo John :)
    Tuesday, June 29, 2010 10:20 AM
  • Also, with all those options set to off, the following compiles and outputs the number 200 but fails on the second WriteLine at run-time:

            Dim a
            A = 123
            Debug.WriteLine(a + "77")
            Debug.WriteLine(a + " bad idea")

    --

    Andrew

     

    Tuesday, June 29, 2010 10:21 AM
  • But I disagree with Strict ON and Infer OFF.

    Infer only work under method (like sub or function).

    Noway they can curse any trouble.


    Hi Richter03,

    What settings do you propose then? Most of the experienced programmers on these forums will use and recommend:

     

    OPTION STRICT ON

    OPTION EXPLICIT ON

    OPTION INFER OFF

     

     

    Look how messy code can get if you have OPTION STRICT OFF and OPTION INFER OFF

    One result here is 100 and the other result is 2377

    The variables a and b are both objects too.

    So in the 1st messagebox both objects are added together as numbers of some unknown TYPE.

    In the 2nd they are joined as STRINGs.  That is a deliberate example of how messy things can get.>>

     

     

    Option Strict Off
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
      Dim a = "23"
      Dim b = 77
    
      'Result = 100
      MessageBox.Show(a + b)
    
      'Result = 2377
      MessageBox.Show(a & b)
    
     End Sub
    End Class

     

     

    To Frank,

    Thanks. :-)

     


    Regards, John : Trying hard for another goal into the .Net!!
    Tuesday, June 29, 2010 10:48 AM
  • Recommend having it enabled, however; you will have to turn it off when using late binding for COM libraries.

    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, June 29, 2010 12:24 PM
  • Hi emil_tr ,

    Recommended:

    Option Strict On

    Option Explicit On

    Option Infer Off

    If you run into any problems with these settings in your code, please start a new question. :-)    ;-)


    Regards, John : Trying hard for another goal into the .Net!!

    haha cool, i was working all other way around :)) I think i will change it strict and explicit only but i like infer on. thanks to you all it was great tutorial but my main question was why it should be faster when I use strict on, otherwise i knew what they are for.
    Tuesday, June 29, 2010 12:33 PM
  • Hi again emil_tr,

    Please mark as anwser or / and as helpful ( click the green triangle in a post ) any post you feel full answers

     you question(s) or those that are helpful to you, now and in the future.


    Regards, John : Trying hard for another goal into the .Net!!
    Tuesday, June 29, 2010 5:32 PM