I’m using c#, I know you can streamline the creation of new objects
List<String> myStrings = new List<String>() { "Hello", "GoodBye" };
I also use it for creating new treenodes
node_Clicked.Nodes.Add(new TreeNode() { Text = "New Node" });
Pretty useful, however, I would like to use it for list views however when i start writing it:
folder_listView.Items.Add(
new ListViewItem()
{
Text = "First Col",
SubItems.Add(new ListViewItem.ListViewSubItem()
{
Text = "Second Col"
}),
I get Intellisense error:
Invalid initializer member declarator
So I guess that means you can’t instantiate objects within newly instantiated objects because not all the objects (such as lists) inside the newly instantiated object have been instantiated?
Am I right in thinking that?
Cheers
No, the problem is
SubItems.Add()is a method, not a property/field. Calling a method is very different from initializing a member!That said, you would be able to do
SubItems = ...if it were settable, but as @RB pointed out, it is not.