- Home /
Combining Lists
I have two lists created from classes which contain two string variables each. I want to combine them into a third list, but it keeps giving me overload errors. This third list is going to be sent into a JSON string and saved in a database.
Tell me where I'm blowing it. New List Properties:
public class SheetJSON
{
public string MemberID{get; set;}
public string MemberName{get; set;}
public string EquipmentID{get; set;}
public string EquipmentDescription{get; set;}
public GigSheetJSON(string memberID, string memberName, string equipmentID, string equipmentDescription)
{
this.MemberID=memberID;
this.MemberName=memberName;
this.EquipmentID=equipmentID;
this.EquipmentDescription=equipmentDescription;
}
}
Main Script:
public List<Equipment> Equipment;
public List<Members> Members;
public List<SheetJSON> Sheet;
void Awake()
{
Equipment=new List<Equipment>();
Members=new List<Members>();
Sheet=new List<SheetJSON>();
}
// Use this for initialization
void Start ()
{
EnableButtons();
GetAllEquipment();
GetAllMembers();
}
void Testing()
{
Sheet.AddRange(Members);
Sheet.AddRange(Equipment);
Sheet.Sort();
}
Errors: The best overloaded method match for System.Collections.Generic.List<SheetJSON>.AddRange(System.Collections.Generic.IEnumerable<SheetJSON>)' has some invalid arguments Argument
#1' cannot convert System.Collections.Generic.List<Members>' expression to type
System.Collections.Generic.IEnumerable'
Looks like I'm not completing the interface, but how do I implement it?? I had hoped to have them nested so that I could show the Member and the equipment they use. Hopefully this is clear enough. Thanks in advance!
All 3 of your lists contain different datatypes (SheetJSON, $$anonymous$$embers, Equipment). You won't be able to combine them into a single list.
That said, without knowing the details of "$$anonymous$$embers" and "Equipment", it's difficult to offer any more advice.
The same as my advice on the last question... $$anonymous$$ake a custom struct and build your new list by placing the elements from both into the new struct and Adding that to a List.
You cant add a 'Vector3' to a List<int>
in the same way you cant add a '$$anonymous$$embers' to a List. The data types are simply different and incompatible.
No, the lists contain "$$anonymous$$embers" and "Equipment" objects - according your code. Now, those objects may only contain string members (we don't know as you don't show that make-up of those objects) - but it doesn't really matter. You can't combine dissimilar objects ($$anonymous$$embers and Equipment) into a single, common list.
Sheet.$$anonymous$$embers.AddRange($$anonymous$$embers);
would eli$$anonymous$$ate the cast error, and add to the $$anonymous$$embers List but I'm not sure that what you really want to do. (p.s. confusing to have classes and member variables with the same name and case.)
I suspect you probably want to do something like the below, to get all members into the sheet list. I'm making a TON of assumptions below, it' just an example.
for(int i=0;i< $$anonymous$$embers.count;i++)
{
Sheet.Add(new GigSheetJSON( $$anonymous$$embers[i].id, $$anonymous$$embers[i].name, Equipment[i].id ...));
}
So AddRange only works for primitive types not for classes? You have to loop through both lists to merge them together?
Ideally, I would like to set it up in a way that it displays the name of the $$anonymous$$m member and the equipment they use: gun, knife, super terrific weapon, under their name. $$anonymous$$gestions would be helpful.
Add range does indeed work for adding LISTS of classes in addition to primative types. But notice that new GigSheetJason(..)
only instantiates and returns a SINGLE instance of a SheetJSON class. It is this single instance that contains the lists of equipment and members (at least that how you defined it). Your definition ALSO contains a list of SheetJSON's, but how you want to use and initialize that, I'm not sure (but I suspect you don't actually want it defined INSIDE the SheetJSON class. You probably DO want a list of SheetJSONs in your "Team" class though...).
Answer by JoshuaMcKenzie · Apr 16, 2016 at 05:18 PM
I recommend in making both Equipment and Member implement an interface and just have the list store a collection of that interface. It take very little code and promotes keeping Member and Equipment decoupled. then you can JSON out the list as the interface. then use a factory class to reconstruct the classes back when you want to use them.
the question in your link is different from your problem. is problem was adding multiple items of the same type into a list with separate parameters, you problem was adding different items into one list.
you'd still need a way to composite your two types together. thats why I recommended applying an interface that could convert your objects into JSON strings (again with the JSON also including the name of the original Object type so that you could use a factory to remake it).
you could have also made adapter/wrapper classes that basically would do the same thing as the interfaces, and they work just as well, but they take more code, and induce stronger coupling
Both lists contain all strings, but I put them in separate classes, which makes it more difficult to combine. I'm thinking about using LINQ to combine them. Anybody have thoughts on that?
LINQ doesn't have any mystical powers that allow it to do anything you can't already do with non-LINQ code. It sometimes allows for a more elegant solution, but there's really no magic there.
So, if you can't solve the problem without LINQ, I don't think you'll solve it with LINQ.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
check if list contains item with matching string property 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Instantiate prefab at certain positions 3 Answers