Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 IMoriarty · Dec 06, 2011 at 07:15 AM · c#listinventory

Lists as Parameters?

So, working through my project I stumbled upon a number of answers here that suggested that when possible I should use Lists instead of ArrayLists to hold data, so I went through and converted the various sections of code to support a List.

Then I got stuck. :(

I'm pretty new to C#, Unity and scripting in general, so pardon Newbie mistakes!

 public string ListContents<inputType> (List<inputType> iList)
 {
     string invList = null;
     if(iList != null){
         foreach (string item in iList) {
             invList = string.Concat (invList, " ", item);
         }
         if(invList != null){
             invList=invList.Trim();
         }
     }
     return invList;
 }

Code is intended to to take in an Item Inventory from my Player Manager script as a param, generate a concat string of the content and chuck it back out as the return for the GUI function calling it later on.

 GUI.Label (new Rect (10, 50, 250, 100), ListContents (PlayerManager.ItemInventory));

Problem is with the function's first line, I get various errors based on the variation I try with no real understanding of which I should be working with and why they're throwing errors.

I started with:

 public string ListContents (List iList)

Error CS0246: The type or namespace name List' could not be found. Are you missing a using directive or an assembly reference? Which gave me the reminder to add "using System.Collections.Generic;" to the top of my code, but still it persisted. I found some answers leading down the "Generic" route: public string ListContents<string> (List<string> iList) Error CS0081: Type parameter declaration must be an identifier not a type public string ListContents<inputType> (List<inputType> iList) Error CS0030: Cannot convert type inputType' to `string'

But ultimately I don't really want a generic, I chose Lists to be strongly typed, so why circumvent that?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by syclamoth · Dec 06, 2011 at 07:35 AM

Generic lists are still strongly typed- the reason you are getting all these errors is because you are treating them like they aren't!

The main problem here, is that you are using a generic method, and then assuming that the list will always be a list of strings. It doesn't like that, because it needs to remain as general as it can possibly be. In your case, you should just specify the desired type and be done with it.

 public string ListContents (List<string> iList)

Of course, since every type must have a 'ToString' method, you could still do it like this-

 public string ListContents<inputType> (List<inputType> iList)
 {
     string invList = null;
     if(iList != null){
        foreach (inputType item in iList) {
          invList = string.Concat (invList, " ", item.ToString());
        }
        if(invList != null){
          invList=invList.Trim();
        }
     }
     return invList;
 }
Comment
Add comment · Show 7 · 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 IMoriarty · Dec 06, 2011 at 07:40 AM 0
Share

Right - I attempted to add the typing but it seems I was a little overzealous when I did so by adding (string) to ListContents. I think I just misunderstood what needed the (string), so when that threw an error I backed off. Thanks for the answer!

PS: How do I add angle brackets to a comment?

avatar image CHPedersen · Dec 06, 2011 at 07:49 AM 0
Share

D'oh. I hate getting ninja'ed. >_<

avatar image CHPedersen · Dec 06, 2011 at 07:52 AM 0
Share

(I've deleted my reply below. It said basically the same as this, so I wasn't really adding value. Sorry about emails sent)

avatar image CHPedersen · Dec 06, 2011 at 08:28 AM 2
Share

Ins$$anonymous$$d of providing a new answer, the contents of which is a copy of the above, I'll use comments here to describe what I thought didn't make sense. ;)

It's the null-checking that doesn't make sense to me. Checking that the input argument is null implies that your code is calling this method somewhere in a fashion that risks passing null as its input, i.e. string concatenatedList = ListContents(null);

Does that ever happen anywhere in your code? There's no reason it should, so there is no reason to check if the input argument is null. You mentioned in a comment that got emailed to me that the null-checking occurs because the List is initially empty, as the player's inventory starts out empty. But as long as you've instantiated it first with the new-keyword, a List doesn't become null when empty - it's just empty. :) If the player doesn't have any items in his inventory, you can just pass in a reference to his List of items anyway, it just won't contain any items at all, and the foreach-loop will execute 0 times.

In addition, if you set the return string to null initially, but the player doesn't have any items, then this method will return the string unedited, i.e. it will return null. Does this correctly reflect an empty inventory? GUI.Label doesn't crash if it is fed a null-string, so that probably won't be an issue for you. :) But if you use the returned string elsewhere, it may throw NullReferenceExceptions later on to have it return null now. Perhaps it's safer to set the string to "" to begin with, ins$$anonymous$$d of null?

So, all in all, for this method, I would just do this:

 public string ListContents(List<string> iList)
 {
     string invList = "";
     foreach (string item in iList)
         invList = string.Concat(invList, " ", item);        
     return invList.Trim();
 }
avatar image IMoriarty · Dec 06, 2011 at 08:42 AM 0
Share

Right, okay, that totally makes sense.

I got into the habit of checking for nulls since that's what most of my errors tend to be thus far - so the rampant null checking is pretty endemic to my code as a result.

I suppose really need to find a better coding practice, but several of my methods are often called when the input might be null, such as the above code, when I'm waiting for something to happen to trigger a method.

Elsewhere - as a newbie I don't know enough about the syntax to be anything but rather verbose, so things like not needing curly braces on one line foreach loops is news to me.

Ah, so much to learn. :p

Thanks so much for your help!

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Add random amount of random items from one list to another? 2 Answers

How to ignore base class? 1 Answer

check if list contains item with matching string property 2 Answers

Method is called, but GUI doesn't show up 1 Answer

Inventory Script. List Contains. I dont know the need bit of code.[UNSOLVED] 2 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