views:

1408

answers:

3

Is there a VB.NET equivalent to the C# var keyword?

I would like to use it to retrieve the result of a LINQ query.

+14  A: 

Omitting the type in VB.NET (VB9) will implicitly type the variable.

This is not the same as "option strict off" in previous versions of VB.NET, as the variable is strongly-typed, it's just done so implicitly (like the C# var) keyword.

Dim foo = "foo"

foo is declared as a String.

Option Infer must be on in order for this to function properly

Adam Robinson
+2  A: 

Simply use the conventional Dim keyword without a type.

But beware that you need to enable both Option Strict and Option Infer to make this work, otherwise the code either will not compile (Strict On, Infer Off) or will declare the variable as of type Object, which isn’t what you want (Strict Off).

Minimal working example:

Option Strict On
Option Infer On

Imports System

Module MainModule
    Sub Main()
        Dim i = 42
        Dim s = "Hello"
        Console.WriteLine("{0}, {1}", i.GetType(), s.GetType())
        ' Prints System.Int32, System.String '
    End Sub
End Module
Konrad Rudolph
+6  A: 

You need Option Infer On and then just use the Dim keyword, thus:

Dim query = From x In y Where x.z = w Select x

Contrary to some of the other answers, you do not need Option Strict On.

If you're using the VS IDE you can just hover over the variable names, but to get the compile-time types of variables (GetType(variableName) does not compile - "Type '<variablename>' is not defined." - and VarType(variable) is actually just the VB version of variable.GetType() which returns the type of the instance stored in the variable at runtime) I used

Function MyVarType(Of T)(ByRef Var As T) As Type
    Return GetType(T)
End Function

In detail:

  • without Dim:

    Explicit Off, gives Object

    Explicit On, error "Name '' is not declared."

  • with Dim:

    • Infer On, gives expected types
    • Infer Off:

      Strict On, error "Option Strict On requires all declarations to have an 'As' clasue."

      Strict Off, gives Object

Mark Hurd
You can simply use `x.GetType()` in VB – I hadn’t tested my answer’s code, hence the mistake in my old answer. This actually yields the runtime type which can differ from what you get using `GetType(T)`, though. Finally, `Strict On` if course not required for this to work, but should be always on, anyway, and may prevent mistakes if the programmer has forgotten to specify `Infer On`.
Konrad Rudolph
I agree `Option Strict On` is good practice, but `x.GetType` doesn't provide the answer to the question asked here. I haven't tested it, but with `Infer Off` and `Strict Off` a simple `Dim query = From ...` example may work (although the guts of Linq queries may need some of the other effects of `Strict On` or `Infer On` to work correctly) and `query.GetType()` will return the anonymous type, not `Object`, which query will be declared to be.
Mark Hurd
@Mark: Thanks for the correction; I've altered my answer to reflect this adjustment. The wording of the MSDN docs is awkward and seems to imply that `Strict` is required, but they don't actually say that.
Adam Robinson