0

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));

        }
    }
}
2
  • The problem is here: new TestObject().UniqueID(1)); - should be new TestObject().UniqueID = 1; UniqueID is a property, not a method, and in the original code it's being used like a method. Commented Sep 19, 2013 at 1:54
  • If I do that I then get "Invalid arguments". Commented Sep 19, 2013 at 1:57

5 Answers 5

2

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
});
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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));

        }
    }
}

Comments

1

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.

Comments

0
 TestObject TestObject = new TestObject();

Remove this line.

And edit following lines to this syntax

new TestObject { UniqueID = 39 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.