-1

The following code attempts to create a data struct for a wall that has a list of members and those members can further have a list of sub-members. When adding the sub-member list to a member's sub-member list field I get the compile error:

Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List' to 'HeaderViewer.CurrentHeaders.WallSubcomponentMember' HeaderViewer

I think this means that I'm trying to assign a list of type WallSubcomponentMember to just a single WallSubcomponentMember. But my WallMember class field SCMembers is a list of WallSubcomponentMember.

Why does the compiler think SCMembers is not a list?

 public class Wall
    {
        public Single WallNum;
        public List<WallMember> Members;
    }

    public class WallMember
    {
        public Single MemId;
        public List<WallSubcomponentMember> SCMembers;
    }

    public class WallSubcomponentMember
    {
        public Single MemId;
    }

    private List<Wall> TestData ()
    {
        var wallList = new List<Wall>();

        var Member1 = new WallMember
        { MemId = 1 };

        var SubcomponentMember1 = new WallSubcomponentMember
        { MemId = 1 };
        var SubcomponentMember2 = new WallSubcomponentMember
        { MemId = 2 };
        var subcomponentMemberList = new List<WallSubcomponentMember>();
        subcomponentMemberList.Add(SubcomponentMember1);
        subcomponentMemberList.Add(SubcomponentMember2);

        Member1.SCMembers.Add(subcomponentMemberList);

        var memberList = new List<WallMember>();
        memberList.Add(Member1);

        var aWall = new Wall
        {
            WallNum = 1,
            Members = memberList
        };
        wallList.Add(aWall);
        return wallList;
    }
4
  • It doesn't think SCMembers isn't a list. The issue is that you are trying to Add() a List<WallSubcomponentMember> to a List<WallSubcomponentMember>. Did you mean to add SubcomponentMember1 and SubcomponentMember2 directly to Member1.SCMembers? Or since SCMembers is a field, not a get-only property, you could also just do Member1.SCMembers = subcomponentMemberList;, but I would not recommend that. Apr 7, 2020 at 22:49
  • If it were me, I'd add an Add(WallMember) to Wall and an Add(WallSubcomponentMember) to WallMember. I'd also make them implement the appropriate IEnumerable interfaces. That way, it makes for easier code, and it also allows you to use the collection initialization syntax (if you get it exactly right)
    – Flydog57
    Apr 7, 2020 at 22:59
  • I am curious as to why you are using Single for what appears to be an identifier; normally we'd use int for that. What's the purpose of using a single-precision real here? Apr 7, 2020 at 23:07
  • I thought int wasn't big enough handle my rather large id numbers but after checking int is indeed the way to go. Thanks! Lot's of refactoring to do once I get this working.
    – Killakeys
    Apr 7, 2020 at 23:40

1 Answer 1

3

I think this means that I'm trying to assign a list of type WallSubcomponentMember to just a single WallSubcomponentMember.

That is very close. Read the error message again:

Argument 1: cannot convert 

Though this is an assignment -- specifically it is an assignment to a formal parameter -- you are better off thinking of it as a failed conversion. Many conversions are due to assignments, but it is the conversion that matters.

But my WallMember class field SCMembers is a list of WallSubcomponentMember.

Correct.

Why does the compiler think SCMembers is not a list?

The question cannot be answered because it makes a false assumption. The compiler does think that SCMembers is a list; in fact, it would not give the error if it could not!

Again, look at the error message. Carefully reading error messages will pay dividends in the future. The error is on argument one. In the line:

   Member1.SCMembers.Add(subcomponentMemberList);

there is only one thing that could be "argument one" and that is subcomponentMemberList. We then look up the documentation for Add and see that in a List<T>, the Add method takes T, not List<T>. Now the error message should make sense; List<WallSubcomponentMember>.Add takes a WallSubcomponentMember, but you passed a List<WallSubcomponentMember>, and there is no conversion from what you passed to what Add needs, and so an error message is given on the first (and only) argument.

You likely intended to call AddRange, which adds the contents of one list to another list. That said, looking at your code it is not clear why you have created an auxiliary list at all; why not simply add the subcomponents directly to the eventual destination? You never use subcomponentMemberList again, so it appears to be useful for nothing in particular.

While we are looking at your code: get out of the habit now of making public fields; they are a bad practice in C#. If you intend to have a public field, make it a public property instead:

public Single WallNum { get; set; }

and then consider whether the property actually needs to be read-write; my preference is to make read-only properties or properties that can only be written by the containing class:

public Single WallNum { get; private set; }

for example.

4
  • 1
    Thanks! I think I'll go apply for truck driver school now. Jeez, I should have seen that. I certainly don't need the auxiliary list. Thanks for the reminder about the public fields. My actual code is scoped correctly (I think)!
    – Killakeys
    Apr 7, 2020 at 23:25
  • 1
    @Killakeys: Glad you figured it out. Don't be too hard on yourself; it can be difficult to see the problem. And there is an argument to be made that if you could not understand the problem from the error message then the fault is with the error message; error messages tend to be designed by expert programmers who phrase the error in terms of the specification, rather than phrasing them in the way that teaches the developer how to understand the problem. Apr 8, 2020 at 20:54
  • @Killakeys: I am about to embark upon designing the error reporting system for a novel DSL and this is on my mind a lot. The developers who will be using the DSL are data scientists first and programmers second, which presents a great many challenges in designing error messages that genuinely help. Apr 8, 2020 at 20:56
  • 2
    Note that Eric's example, public Single WallNum { get; private set; }, is a property that can only be written by the containing class. A read-only property that can only be written by the containing class would be defined using the syntax, public Single WallNum { get;}.
    – Brian
    Apr 8, 2020 at 21:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.