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;
}
SCMembers
isn't a list. The issue is that you are trying toAdd()
aList<WallSubcomponentMember>
to aList<WallSubcomponentMember>
. Did you mean to addSubcomponentMember1
andSubcomponentMember2
directly toMember1.SCMembers
? Or sinceSCMembers
is a field, not aget
-only property, you could also just doMember1.SCMembers = subcomponentMemberList;
, but I would not recommend that.Add(WallMember)
toWall
and anAdd(WallSubcomponentMember)
toWallMember
. 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)Single
for what appears to be an identifier; normally we'd useint
for that. What's the purpose of using a single-precision real here?