C
C#14mo ago
shuffle

✅ Need some help with lists please

https://github.com/Kuba-Kowal/Text-Based-Game/blob/main/Program.cs <- code can be found here I am trying to create a list that i can add items to however, the bools work fine but nothing will add to the list for some reason. any help would be greatly appreciated
GitHub
Text-Based-Game/Program.cs at main · Kuba-Kowal/Text-Based-Game
Contribute to Kuba-Kowal/Text-Based-Game development by creating an account on GitHub.
16 Replies
shuffle
shuffleOP14mo ago
i have tried putting the list inside the main function but that gives me an error as well as replacing
List<string> inventory = new List<string>() {""};
List<string> inventory = new List<string>() {""};
with
List<Items> inventory = new List<Items>() {""};
List<Items> inventory = new List<Items>() {""};
Jimmacle
Jimmacle14mo ago
your second example won't work because you've specified the list holds instances of an Items class and "" is a string, which isn't a compatible type what exactly do you mean by "nothing will add to the list?" do you have an example or specific part of this code that shows the problem?
shuffle
shuffleOP14mo ago
yes bare with me
Jimmacle
Jimmacle14mo ago
also, i really suggest looking into multiline string literals to make your console printing less... terrible to read https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-11.0/raw-string-literal
shuffle
shuffleOP14mo ago
the 2nd part of code i mean would be with
List<Items> inventory = new List<Items>();
List<Items> inventory = new List<Items>();
so when i "open the chest and find some gloves" i want the gloves to be added to the inventory
shuffle
shuffleOP14mo ago
No description
shuffle
shuffleOP14mo ago
but i get this instead
Jimmacle
Jimmacle14mo ago
that doesn't mean nothing was added that's what happens when you call ToString on a list if you want to print the contents, you should do something with string.Join or a loop
shuffle
shuffleOP14mo ago
okay thats one of my issues out the wya the only reason im printing it is to troubleshoot however the main issue is that line 138 doesnt work even after "adding the gloves" it instead calls line 125
Jimmacle
Jimmacle14mo ago
yeah, that's not a problem with your list that's a problem with the way you've ordered your logic
shuffle
shuffleOP14mo ago
oh okay
Jimmacle
Jimmacle14mo ago
if if (subMenuSelect == "take") is true it will take that branch and stop evaluating the other nested if statements which means it will never run your second one because the first will always be true if the second is true
shuffle
shuffleOP14mo ago
i am such an idiot i didnt even think of that aha. thank you very much just needed a second pair of eyes i guess
Jimmacle
Jimmacle14mo ago
also, in the future using a debugger instead of printing things to the console is a big help if you set a breakpoint at a line in your code you can check the state of all the variables in scope at that point, like seeing what items are in your list https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-debugger?view=vs-2022
shuffle
shuffleOP14mo ago
amazing thank you ill have a read !close
Accord
Accord14mo ago
Closed!
Want results from more Discord servers?
Add your server