Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops.

VS Designer: 'X' could not be set on property 'Y'

1 min read

I'm currently developing some custom ASP.net server controls. Internally I often have lists, where the user (programmer) can specify a variable number of sub-controls which are consumed/managed by the parent custom server control:
public List<SomeControl> SomeControls
{
get
{
...
}

set
{
...
}
}

This kind of list is used for controls where the user (programmer) specifies something like
<mySrvCtrls:MyServerControl id="mySrvControl1" runat="server" ...>
<mySrvCtrls:SomeControl id="...1" runat="server" ... >
<mySrvCtrls:SomeControl id="...2" runat="server" ... >
<mySrvCtrls:SomeControl id="...3" runat="server" ... >
</mySrvCtrls>

They're already working, but now I'd like to add some VS designer support, since when switching to Design-View my controls throw the error
'SomeControl' could not be set on property 'SomeControls'
Apparently the problem is that collections of controls such as my "SomeControls" (dummy) property here, have to be specified as readonly, meaning to change them to something like
public List<SomeControl> SomeControls
{
get
{
...
}

}

But still, in such a case you have to pay attention to ensure in your get that you're properly instantiating the collection you're returning if it's null.
public List<SomeControl> SomeControls
{
get
{
if(_SomeControls == null)
_SomeControls = new List<SomeControl>();

return _SomeControls;
}

}

This solved my design-time error.
Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus