- Home /
C# List tutorials & required code
I need a good C# understanding of Lists as I want to perform 2 functions on my List and I don't know how to. I do not want to be spoon-fed the answers because I will not have a understanding. Unity's tutorial on Lists and .NET are to much for me, I do not understand anything they say.
So where can I find out more about Lists so I can perform the functions of Add and display all values of lists while using a item class (The List is a Inventory)
[EDIT] Ok im sorry I was abit frustrated while typing this and now ive calmed down. I will retype this to make it less hostile.
"Hey guys! I want to learn how to use lists, but I refuse to use anything that $$anonymous$$ches me how to use lists!"
Seriously, you sound like that. The .NET libraries have very good documentation, including a list of available methods, which you can find here: Link
If you find something on that page that you don't understand, then search for it to try and find an explanation. You will not ever get good at program$$anonymous$$g if you just learn how to do what you need at the time - you need to understand the underlying concepts as well, and that will take time. You should not expect to be able to jump straight into things without having to spend a lot of your time learning.
@Hoeloe Ok im sorry I was abit frustrated while typing this and now ive calmed down.
I find .NET to be abit to much and its throwing to much at me.
@Stormizin Erm....C# Unity
Answer by HuskyPanda213 · Feb 20, 2014 at 08:04 PM
Do not throw aside the unity tutorials they are very good. In short, List.Add works by putting the name of your list(say items), and then doing the add function which takes in the type of class the list has, and then adds it.
List<Item> Items = new List<Item>();
Item AItem = new Item();
AItem.Value = 1;
Items.Add(AItem);
And so on...
Next, displaying a list, this can be achieved with foreach and for loops(with for loops you need to put the item of the list you want to view in [], but that number is usually the number the for loop operates from).
If you do not know how to use foreach loops here is an example/explanation. the loop has the structure
List<ClassTypeListUses> TheListToUse = new List<ClassTypeListUses>();
foreach(ClassTypeListUses TempItem in TheListToUse){
GUILayout.Label(TempItem.Value.ToString());
}
ClassTypeListUses is what you assigned the lists value to(the class type that is in the ). TempItem is what the item your refering to is, the instance. in is just part of the loop, but IT HAS TO BE THERE, and TheListToUse is the list to get the items from.
for example
for(int i = 0;i<TheListToUse.count;i++){
GUILayout.Label(TheListToUse[i].Value.ToString());
}
And there you have it!
What is AItem? Also what is each line doing. If it helps my List is called Inventory, my item class is called WorldItemProperties and I wish to access my items by their ItemName variables.
Could you run me through the last 3 lines of code please?
Cheers!
LAst three lines of code, a basic forloop that ends at the lists count variable, imagine listtouse has a name/value, that is what value is, and then tostring just makes it a string, this all makes several labels that display each items name.
And AItem would just be an instance of 1 item(in your case WorldItemProperties), which gets added to a list.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I get just one componente, or except one, from a list? (string.Join) 0 Answers
Foreach with where with 2 lists 0 Answers