Reputation: 6180
I'm not sure how to create a list of objects. I get "Non-invocable member ListObjects.TestObject.UniqueID' cannot be used like a method."
Any insight would be helpful.
The code for the object I'm trying to create a list of:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListObjects
{
class TestObject
{
public int UniqueID { get; set; }
}
}
Main code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListObjects
{
class Program
{
static void Main(string[] args)
{
TestObject TestObject = new TestObject();
List<TestObject> ObjectList = new List<TestObject>();
ObjectList.Add(new TestObject().UniqueID(1));
ObjectList.Add(new TestObject().UniqueID(10));
ObjectList.Add(new TestObject().UniqueID(39));
}
}
}
Upvotes: 0
Views: 471
Reputation: 1228
You are using UniqueId like a method. It is a property and must be assigned. If you know you'll be assigning an ID when creating a TestObject, you should make the constructor support that, and use it accordingly. If not, use ObjectList.Add(new TestObject { UniqueID = 1 });
to assign the value without a constructor.
This is how I would handle the situation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListObjects
{
class TestObject
{
public int UniqueID { get; set; }
public TestObject(int uniqueId)
{
UniqueID = uniqueId;
}
}
}
Main code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListObjects
{
class Program
{
static void Main(string[] args)
{
List<TestObject> ObjectList = new List<TestObject>();
ObjectList.Add(new TestObject(1));
ObjectList.Add(new TestObject(10));
ObjectList.Add(new TestObject(39));
}
}
}
Upvotes: 2
Reputation: 28530
There's a couple of things going on here. You don't need an instance of the object to declare the list, just the type.
Also, UniqueID
is a property, not a method. You assign values to them (or read values from them), you don't pass in values like a parameter for a method.
You can also initialize the list in one call, like this:
List<TestObject> ObjectList = new List<TestObject>()
{ new TestObject() { UniqueID = 1},
new TestObject() { UniqueID = 10 },
new TestObject() { UniqueID = 39 } };
This will result in a new List<T>
where T
is of type TestObject
, and the list is initialized to 3 instances of TestObject
, with each instance initialized with a value for UniqueID
.
Upvotes: 1
Reputation: 3459
I cannot rememer the syntax but it is from memory:
repalce this line:
ObjectList.Add(new TestObject().UniqueID(1));
with this line:
ObjectList.Add(new TestObject(){UniqueID = 1});
and do the same for all .Add lines you have.
Upvotes: 2
Reputation: 3500
TestObject TestObject = new TestObject();
Remove this line.
And edit following lines to this syntax
new TestObject { UniqueID = 39 }
Upvotes: 0
Reputation: 317
public int UniqueID { get; set; } is not a method its a setter and you use it like a method
do this
ObjectList.Add(new TestObject()
{
UniqueID = 1
});
Upvotes: 2