10

I really like the coding speed that VB.NET provides, but I don't like the possibility to forget to declare variable types, return types of functions, etc. and that is why in each class I use:

Option Explicit On  
Option Strict On

Is there a way to define those two options on the project/solution level?
It is really tedious to copy those two options in each class...
We code with Visual Studio 2010 in .NET 4 Client Profile.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
This is it
  • 779
  • 2
  • 11
  • 23

2 Answers2

16

Yes, you can set these options for the entire project in your project's Properties:

  1. Right-click on your project in the Solution Explorer, and select "Properties" from the context menu.
  2. Click on the "Compile" tab in the list on the left-hand side.
  3. Set the values of the comboboxes as desired.

   Project properties - Compile tab - Compile Options


You could also choose to specify these settings globally in the Visual Studio options (although this will only affect new projects, not existing ones):

  1. In the Visual Studio environment, click on the "Tools" menu and select "Options".
  2. Expand the "Projects and Solutions" item in the treeview on the left-hand side of the dialog.
  3. Select the "VB Defaults" item.
  4. Set your "Default project settings" as desired.

   VS Options - Projects and Solutions - VB Defaults - Default project settings

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 6
    +1: I strongly recommend having these set globally, and I mean **REALLY STRONGLY RECOMMEND** . . . – Binary Worrier Feb 22 '11 at 10:34
  • Yes, I agree with Binary Worrier. Your settings should probably look **exactly** like the ones shown here. (Although some might prefer `Option Compare Text`.) – Cody Gray - on strike Feb 22 '11 at 10:35
  • 1
    Is there a specific reason to not put Option Infer On? – jeroenh Feb 22 '11 at 12:41
  • @jeroenh: I discussed that very issue at length with another member recently. It seems that with later versions it's perfectly possible to write good code with `Option Infer On`, but I personally have seen too many mistakes made by leaving it on for my comfort. I prefer to work with as strict of type checking turned on as possible. I'm not sure what the harm is in being explicit; I've seen the harm in *not* being such. Ultimately I suppose it's a gut call. – Cody Gray - on strike Feb 22 '11 at 12:45
  • @Cody Gray As long as Option Strict On, I have no problem with Option Infer On. The biggest advantages to me are easier refactoring and (somewhat) less verbosity. – jeroenh Feb 22 '11 at 13:02
  • 1
    +1 Binary worrier. VB.Net is not installed until you have set `Option Explict On` and `Option Strict On`. I don't usually **SHOUT** much, but **I DO ABOUT THIS** – MarkJ Feb 22 '11 at 13:04
  • 1
    @jeroenh @Cody Gray The major advantage of `Option Infer On` is that you can use Linq – MarkJ Feb 22 '11 at 13:07
  • @MarkJ: Oh, is *that* it? That's good to know. I couldn't figure out why everyone always seems to resist my preference to have it off. I don't find myself using Linq very often. – Cody Gray - on strike Feb 22 '11 at 13:13
  • 1
    @Cody Gray Yes, in fact type inference was added to the language [specifically in order to](http://msdn.microsoft.com/en-us/magazine/cc163345.aspx#S1) make Linq possible. Disclaimer: I don't use Linq much either... one day I must try it out properly and find out what the fuss is about :) – MarkJ Feb 22 '11 at 13:20
  • A point noone's mentioned: Your `Option Strict On`, etc in existing files will override any project-level settings (which is why they worked, of course). – Mark Hurd Feb 26 '13 at 07:28
2

Well, the obvious place is to look in the project options dialog, and lo and behold:

Visual Basic .NET Project Options

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825