Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gilead7 · Apr 14, 2016 at 11:30 PM · c#listproperties

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!

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jgodfrey · Apr 14, 2016 at 11:54 PM 1
Share

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.

avatar image meat5000 ♦ · Apr 15, 2016 at 08:22 AM 0
Share

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.

avatar image Gilead7 · Apr 15, 2016 at 06:22 PM 0
Share

Both lists only have strings.

avatar image jgodfrey Gilead7 · Apr 15, 2016 at 06:40 PM 1
Share

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.

avatar image Glurth · Apr 15, 2016 at 06:50 PM 0
Share

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

avatar image Gilead7 · Apr 16, 2016 at 04:56 PM 0
Share

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.

avatar image Glurth Gilead7 · Apr 16, 2016 at 10:32 PM 0
Share

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

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gilead7 · Apr 18, 2016 at 06:31 PM 0
Share

Thanks Josh!

avatar image Gilead7 · Apr 21, 2016 at 12:50 AM 0
Share

OR... Should I look into something like this.

link text

avatar image JoshuaMcKenzie Gilead7 · Apr 26, 2016 at 02:21 PM 0
Share

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

avatar image Gilead7 · Apr 29, 2016 at 07:23 PM 0
Share

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?

avatar image jgodfrey Gilead7 · Apr 29, 2016 at 07:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges