Member '<membername>' cannot be initialized in an object initializer expression because it is shared

Object initializers cannot be used to initialize any member of a class that is declared as shared. For more information, see Shared (Visual Basic).

Error ID: BC30991

To correct this error

  1. Examine the class definition to determine which member is shared.

  2. Eliminate the initialization for that member from the object initializer list.

Example

In the following example, totalCustomers is a shared member.

Public Class Customer
    Public Shared totalCustomers As Integer
    ' Other declarations and method definitions.
End Class

Because totalCustomers is shared, trying to set its initial value in an object initializer list causes this error.

' This declaration is not valid.
' Dim cust As New Customer With { .Name = "Coho Winery", _
'                                 .totalCustomers = 21 }

See Also

Concepts

Object Initializers: Named and Anonymous Types

Shared Members in Visual Basic

Reference

Shared (Visual Basic)