0

I notice that both of these compile without any compiler warnings or errors, even with Option Strict and Option Explicit both turned on:

    Dim x As Exception = New Exception("this is a test")
    Dim y = New Exception("this is another test")

My question is, is it more proper to use the first way (see variable x) or the second way (see variable y)? My guess is that VB doesn't need the As clause since the variable is being initialized in place, so the compiler can infer the type.

I tend to like the first way as it just "feels" right and is more consistent with other languages like C#, just wondered if there was some good reason for one way over the other. I guess it's really personal choice.

1
  • If your question is referring to the usage of implicitly typed local variables I would suggest searching on the usage of var in C#, which would be similar to the Dim y = approach. The same reasoning would apply: stackoverflow.com/search?q=c%23+var+usage Commented Jun 10, 2010 at 14:51

4 Answers 4

4

Behold the wonder of Option Infer On, the compiler figures out the type of "y" automatically. Available since VS2008. You'll get the error you are looking for by turning it off:

Option Strict On
Option Infer Off

Module Module1
    Sub Main()
        Dim x As Exception = New Exception("this is a test")
        Dim y = New Exception("this is another test")   ''# error BC30209
        Dim z As New Exception("this is a third test")
    End Sub
End Module
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer. Is one way considered more "proper" than another way, or is it really up to the programmer?
@dcp - the "proper" way most of the time is a third option you didn't use: Dim x As New Exception("this is an exception")
@dcp: type inference is quite nice with few drawbacks, although your colleagues might find it less readable right now. Don't hesitate to use it. Beware that you cannot use it everywhere, only on local variables.
@Konrad: "cannot use everywhere".
@Joel: ah! double quote. Nobody liked REM. Thanks for the tip :)
2

I'd do Dim x As New Exception("this is a test"). Best of both worlds, no infering but you still only have to type Exception once :)

1 Comment

true; this isn't affected by Option Infer -- but also can only be used when New ing up a variable
2

Option Infer is what controls this compiler feature. Both are equivalent--this is similar to the (moot) C# debate about whether to use the var keyword. My two-cents is to leave it up to the individual developer, however many people will likely say to establish a convention and follow it.

Comments

0

I think the first one (with the variable type declaration) would be the safest to use. If the program is small, it won't really make a difference, but for larger program's, there could be a noticeable compiler lag. So (in my opinion) declaring the type is the best thing to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.