Results 1 to 12 of 12

Thread: [RESOLVED] Option strict

  1. #1

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Resolved [RESOLVED] Option strict

    Hi all,

    very short question... I turned option strict on in the properties window of my solution. Do I have to put option strict on above my code aswell?

    Thnx

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Option strict

    No, setting Option Strict On via your solution properties sets it globally. If you need to fine tune it so you have it on and off depending upon the file, then you can add it to the top of your file that you want to be different from the global setting.

    For ex:

    Turn it on like you did but you have one file where your code needs to have it turned Off. Then you add the declaration to the top of just that file and set it to Off there. It will be off for only that file.

    Conversly if you wanted it to be off globally then set it via the solution properties as Off. Then if you want to turn it on in certain files you can declare it at the top of just those files with the On setting.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: Option strict

    Ok, thank you very much!

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Option strict

    Also, you might want to make it so that it is turned on by default in any new projects you create. You can do this by going to Tools -> Options -> Expand "Projects and Solutions" -> click "VB Defaults" -> Change Option Strict to ON and then click OK
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Option strict

    Hey,

    Can't agree with Chris enough!! This should be on as default, along with Option Explicit and Option Infer.

    Gary

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] Option strict

    I have my VS Default Options for Explicit = On, Strict = On, Infer = Off, the reason I hae Infer off is because it's really only there for Linq of which none of my clients have the 3.5 framework so I can't use Linq anyways.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Option strict

    Its not JUST for LINQ though... It also means you dont have to declare the type of a variable if you assign a value to it at the same time as you declare it.

    E.g:
    vb Code:
    1. Dim X = 10
    2. 'X is actually of Integer type
    3. 'Not Object type as it would be with Option Infer turned off

    I dont understand why you would ever need to turn it off, its not as if it does any harm being turned on. But each to their own
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] Option strict

    Quote Originally Posted by chris128 View Post
    Its not JUST for LINQ though... It also means you dont have to declare the type of a variable if you assign a value to it at the same time as you declare it.

    E.g:
    vb Code:
    1. Dim X = 10
    2. 'X is actually of Integer type
    3. 'Not Object type as it would be with Option Infer turned off

    I dont understand why you would ever need to turn it off, its not as if it does any harm being turned on. But each to their own
    Yes, I know that, that's exactly the nature of Infer. However "Dim X = 10" isn't very readable compared to 'Dim X As Integer = 10" the "As Integer" is self documenting.

    It's not that having it On does harm, it's that unless you're doing Linq having it Off means you have to explicitly declare the variable types is all.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Option strict

    Ah, I take your point.

    That makes sense, however, out of habit I guess I would never write this:

    Code:
    Dim X = 10
    But having Infer On wouldn't throw a warning.

    Gary

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Option strict

    Yeah to be honest I hardly ever use/rely on it because I am used to declaring the types but I do find it useful in some situations.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] Option strict

    Quote Originally Posted by chris128 View Post
    Yeah to be honest I hardly ever use/rely on it because I am used to declaring the types but I do find it useful in some situations.
    What I think sucks is that Linq requires you to Infer things, to me that logically defeats the purpose of declaring things as a specific type, meaning if you don't know what you're getting back how do you know what to declare it and Linq doesn't let you specify the type you're getting back so the variable's Inferred instead. Kinda backwards like the gay guy in an all male school.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  12. #12
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Option strict

    I totally agree, I've hardly used LINQ but when I did that was exactly what I thought... It seems like a real weird way to work when you consider how the rest of the framework works. I dont understand why they had to make it like that but I'm sure there was a good reason.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width