2

I have a class EvoObject.cs which has a constructor defined as:

public class EvoObject
{
    private Object _id;
    private List<List<Int32>> _attributes;

    public EvoObject(Object _id, params List<Int32> _args)
    {
        List<Int32> _attrib = new List<int>();

        Debug.Assert(_args.Count >= 2, "Invalid attributes!");

        this._id = _id;
        _attributes = new List<List<Int32>>(_args.Count);

        for (int _i = 0; _i < _args.Count; _i++)
        {
            _attrib.Add(_args[_i]);
        }

        _attributes.Add(_attrib);
    }
}  

in which I have used params for accepting variable number of parameters.

Now I have another method in another class which returns new EvoObject as:

return new EvoObject(_author, _coAuthors, _papers, _venues);  

in which I want to relate

  • _author with Object _id
  • _coAuthors, _papers, _venues with params List<Int32> _args

in constructor of class EvoObject.cs whereas _coAuthors, _papers and _venues: all are lists of type List<Int32>

I'm getting error at line:

return new EvoObject(_author, _coAuthors, _papers, _venues);  

i.e. EvoObject does not contains a constructor that takes 4 arguments.

How params can be useful here?

5
  • params keyword requires a single dimension array, you can't use List<T>. Commented May 24, 2016 at 13:35
  • I'm more concerned with how in the heck a List<List<Int32>> is going to tell you which identifiers are for what things (papers, venues, etc.). Is that information really not important? What use do those integers become when it's not known what they represent? It seems like some sensible data structures could solve a lot of problems here, including the one being asked. Commented May 24, 2016 at 13:37
  • @David the structure List<List<Int32> the first list will store number of attributes that is 3 for each Object id (i.e. _author) and the nested list will store attribute values for each attribute (i.e. _papers, _coAuthors, _venues) Commented May 24, 2016 at 13:46
  • @Taufel: Well, if it works for you then I guess go with it. Experience tells a very different story though. Consider a famous quote on the subject... "Smart data structures and dumb code works a lot better than the other way around." - Eric S. Raymond Commented May 24, 2016 at 13:49
  • @David: well, if you grabbed the idea I wanted to implement, you can suggest your way too, your courtesy! Commented May 24, 2016 at 13:51

1 Answer 1

5

I believe you're missing the [] on the List declaration in your method signature in order to declare it as a parameter array.

Try public EvoObject(Object _id, params List<Int32>[] _args)

Sign up to request clarification or add additional context in comments.

8 Comments

@DavidY--but making the change suggested by you, I got another error at the statement i.e. _attrib.Add(_args[_i]);. The error is "has some invalid arguments"
@DavidY--by declaring as List<Int32>[] _args, will _args be a List or an Array?
@Taufel _args will be an array of lists. In that case I think you'd want _attributes.Add(_args[_i]);
@DStanley fine, and what about subsequent error I commented above?
@Taufel _args[_i] is a List<int> There should be no problem adding it to a List<List<int>>.
|

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.