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

Visual Basic.NETASP.NET

Avatar of undefined
Last Comment
Corey Scheich

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Corey Scheich

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Corey Scheich

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck