|
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! 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. 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: 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: Error CS0081: Type parameter declaration must be an identifier not a type Error CS0030: Cannot convert type But ultimately I don't really want a generic, I chose Lists to be strongly typed, so why circumvent that?
(comments are locked)
|
|
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. Of course, since every type must have a 'ToString' method, you could still do it like this- 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?
Dec 06 '11 at 07:40 AM
IMoriarty
D'oh. I hate getting ninja'ed. >_<
Dec 06 '11 at 07:49 AM
CHPedersen
(I've deleted my reply below. It said basically the same as this, so I wasn't really adding value. Sorry about emails sent)
Dec 06 '11 at 07:52 AM
CHPedersen
Instead 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, instead of null? So, all in all, for this method, I would just do this:
Dec 06 '11 at 08:28 AM
CHPedersen
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!
Dec 06 '11 at 08:42 AM
IMoriarty
(comments are locked)
|
