Visual Basic Anonymous Types

In visual basic, anonymous types are useful to create an object that contains a set of read-only properties without specifying its type using the New keyword. The name and type for the properties in anonymous type object will be generated by the compiler automatically.

 

Following is the example of creating an anonymous type object with a set of properties in visual basic.

 

Dim userInfo = New With {Key .Id = 1, Key .name = "Suresh", Key .isActive = True}

If you observe the above code, we created an anonymous type object with three properties using New keyword and assigned the object to the variable "userInfo".

 

Here, we need to remember one thing that the expression which we used to assign a value to the property in the anonymous type object cannot be null.

 

As we discussed, the compiler will automatically assign the name and type for the properties in anonymous type object. If you check the following image, when we try to access the properties of an anonymous object, the compiler automatically generated a new name for the anonymous types and applied the appropriate type for the properties based on the value expressions.

 

Visual Basic Access Anonymous Type Properties

 

The compiler has assigned Integer type for Id, String type for Name and Boolean type for IsActive property based on the values which we assigned to respective properties.

Visual Basic Anonymous Type Example

Following is the example of defining and accessing the properties of an anonymous object in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Create anonymous type object

        Dim userInfo = New With {Key .Id = 1, Key .Name = "Suresh Dasari", Key .IsActive = True}

        ' Access anonymous type object properties

        Console.WriteLine("Id:" & userInfo.Id)

        Console.WriteLine("Name:" & userInfo.Name)

        Console.WriteLine("IsActive:" & userInfo.IsActive)

        Console.ReadLine()

    End Sub

End Module

When we execute the above code, we will get the result like as shown below.

 

Id: 1

Name: Suresh Dasari

IsActive: True

Visual Basic Anonymous Type Access Scope

In visual basic, the access scope of anonymous type is limited to the method where it has defined. In case, if we want to send the anonymous type to another method, then that method must accept the parameter of object type but that is not recommended.

 

Following is the example of declaring and sending anonymous type as a parameter to another method using object type in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Create anonymous type object

        Dim userInfo = New With {Key .Id = 1, Key .Name = "Suresh Dasari", Key .IsActive = True}

        ' Sending anonymous type object as parameter

        GetDetails(userInfo)

        Console.ReadLine()

    End Sub

    Private Sub GetDetails(ByVal user As Object)

        Console.WriteLine("Id:" & user.Id)

        Console.WriteLine("Name:" & user.Name)

        Console.WriteLine("IsActive:" & user.IsActive)

    End Sub

End Module

If you observe the above example, we are sending an anonymous type object (userInfo) as a parameter to the method (GetDetails) that accepts a parameter of the object type.

 

When we execute the above example, we will get the result like as shown below.

 

Id: 1

Name: Suresh Dasari

IsActive: True

Visual Basic Nested Anonymous Types

In visual basic, the nested anonymous types can be achieved by defining the anonymous type within another anonymous type as a property.

 

Following is the example of defining the nested anonymous types in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Create nested anonymous type object

        Dim user = New With {Key .Id = 1, Key .Name = "Suresh Dasari", Key .IsActive = True, Key .jobInfo = New With {Key .Designation = "Lead", Key .Location = "Hyderabad"}}

        ' Access anonymous type object properties

        Console.WriteLine("Id: " & user.Id)

        Console.WriteLine("Name: " & user.Name)

        Console.WriteLine("IsActive: " & user.IsActive)

        Console.WriteLine("Designation: {0}, Location: {1}", user.jobInfo.Designation, user.jobInfo.Location)

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we defined an anonymous type object within another anonymous type object as a parameter to achieve nested anonymous type functionality.

 

When we execute the above example, we will get the result like as shown below.

 

Id: 1

Name: Suresh Dasari

IsActive: True

Designation: Lead, Location: Hyderabad

This is how we can achieve nested anonymous type functionality in visual basic based on our requirements.

Visual Basic Anonymous Types in LINQ

In visual basic, anonymous types are mostly useful in Select clause of LINQ query expressions to return the subset of properties from the defined object based on our requirements.

 

Following is the example of defining anonymous types in Select clause of LINQ query expression in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        Dim sInfo As List(Of Student) = New List(Of Student)() From {

               New Student() With {.Id = 1, .Name = "Suresh", .Marks = 300},

               New Student() With {.Id = 2, .Name = "Rohini", .Marks = 500},

               New Student() With {.Id = 3, .Name = "Madhav", .Marks = 400},

               New Student() With {.Id = 4, .Name = "Sateesh", .Marks = 750},

               New Student() With {.Id = 5, .Name = "Praveen", .Marks = 350},

               New Student() With {.Id = 6, .Name = "Sudheer", .Marks = 450},

               New Student() With {.Id = 7, .Name = "Prasad", .Marks = 650}}

        Dim result = From s In sInfo Select New With {Key .StudentName = s.Name, Key .StudentMarks = s.Marks}

        Console.WriteLine("********** Result **********")

        For Each item In result

            Console.WriteLine("Name: {0}, Marks: {1}", item.StudentName, item.StudentMarks)

        Next

        Console.ReadLine()

    End Sub

    Class Student

        Public Property Id() As Integer

        Public Property Name As String

        Public Property Marks As Integer

    End Class

End Module

If you observe the above example, we created a class called Student with different properties and in Main() method, we created anonymous type object in LINQ Select query expression to return only the required fields in result set.

 

When we execute the above example, we will get the result like as shown below.

 

Visual Basic Anonymous Types with LINQ Query Example Result

 

In visual basic, while creating anonymous type object we must need to provide the name for a property that is being initialized with an expression. In case, if we don’t specify the field names in LINQ Select query expression while creating an anonymous type, then the compiler will automatically consider the same name as the property being used to initialize them like as shown below.

 

Dim result = From s In sInfo Select New With {s.Name, s.Marks}

For Each item In result

     Console.WriteLine("Name: {0}, Marks: {1}", item.Name, item.Marks)

Next

 To learn more about LINQ query expressions, refer LINQ Tutorial.

Visual Basic Anonymous Type Overview

Following are the important points which we need to remember about anonymous type in visual basic.

 

  • In visual basic, anonymous types are useful to create an object that contains a set of read-only properties without specifying its type using the New keyword.
  • In an anonymous type, the expression that is used to initialize a property cannot be null.
  • The access scope of the anonymous type is limited to the method where it has defined.
  • In visual basic, anonymous types are mostly useful in Select clause of LINQ query expressions.

Topics Covered