VB.NET equivalent to C# var keyword

C#vb.netLinqKeywordVar

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

Option Infer must be on in order for this to function properly. If so, then omitting the type in VB.NET (Visual Basic 9) 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.

Solution 2 - C#

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

As I mentioned in the comments, there are other reasons why Option Strict On allows Linq to perform more usefully. Specifically, you can't get Into Max(Anon.SomeString) to work with Option Strict Off, though there are a number of workarounds.

Solution 3 - C#

Simply use the conventional Dim keyword without a type.

Minimal working example:

Option Strict On ' Always a good idea
Option Infer On ' Required for type inference

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

Solution 4 - C#

Object worked for me in this example

C#

JToken projects = client.Search(ObjCode.PROJECT, new { groupID = userGroupID });
foreach( var j in projects["data"].Children()) {
        Debug.WriteLine("Name: {0}", j.Value<string>("name"));
}
		  

VB

Dim projects As JToken = client.Search(ObjCode.PROJECT, New With { _
Key .groupID = userGroupID _
})

For Each j As Object In projects("data").Children()
       Debug.WriteLine("Name: {0}", j.Value(Of String)("name"))
Next

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJackView Question on Stackoverflow
Solution 1 - C#Adam RobinsonView Answer on Stackoverflow
Solution 2 - C#Mark HurdView Answer on Stackoverflow
Solution 3 - C#Konrad RudolphView Answer on Stackoverflow
Solution 4 - C#BetoView Answer on Stackoverflow