- Home /
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?
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;
}
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?
(I've deleted my reply below. It said basically the same as this, so I wasn't really adding value. Sorry about emails sent)
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();
}
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!
Your answer
Follow this Question
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