✅ 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
i have tried putting the list inside the main function but that gives me an error
as well as replacing
with
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?yes bare with me
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
the 2nd part of code i mean would be
with
so when i "open the chest and find some gloves"
i want the gloves to be added to the inventory
but i get this instead
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 loopokay 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
yeah, that's not a problem with your list
that's a problem with the way you've ordered your logic
oh okay
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 truei am such an idiot i didnt even think of that aha. thank you very much
just needed a second pair of eyes i guess
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
amazing thank you
ill have a read
!close
Closed!