Link to home
Start Free TrialLog in
Avatar of Ruffone
Ruffone

asked on

Member 'names' cannot be initialized in an object initializer expression because it is shared

How can I ininitialize the "names" property on the "TestPerson" class. The code below throws an error. "Member 'names' cannot be initialized in an object initializer expression because it is shared"

 Return From p In Me.peopleRep.GetAll()
               Let names = Me.nameSvc.GetAll(p.personGuid)
               Select New Credit.Person _
               With {.personId = p.personId,
                     .birthDay = p.birthDay,
                     .gender = p.gender,
                     .names = New ObservableCollection(Of Credit.Name)(names)
                    }

Open in new window


Public Class TestPerson
    Public Property birthDay As String
    Public Property gender As String
    Public Shared Property names As ICollection(Of Name)
        Get
            Return _names
        End Get
        Set(value As ICollection(Of Name))
            _names = value
        End Set
    End Property
    Private Shared _names As ObservableCollection(Of Name) = Nothing
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Corey Scheich
Corey Scheich
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
fyi the reason it throws an error is because it would cause the shared member to be accessed through an instance which is not recommended.

See the Note section on this page.
http://msdn.microsoft.com/en-us/library/4hbsxy95(v=vs.90).aspx


You could also potentially initialize names like this forgive me this is rough
Dim names = Me.nameSvc.GetAll(p.personGuid)
 TestPerson.names =  New ObservableCollection(Of Credit.Name)(names)

Open in new window