• I removed the www from the URL, please update your bookmarks and pay attention to the bottom of the screen if using Google Chrome to allow push notifications again.

Question Go back

porkshopp

Active member
Joined
Apr 13, 2019
Messages
26
Location
Sweden
Programming Experience
Beginner
If I have a method called backpack and want it to display an array of content of my choosing, how can I make it go back to the line of code of which it was executed?
Like this:
C#:
public static void bMenu()
{
    Console.Clear();
    string[] Bp = new string[5];

    Console.WriteLine("Backpack");

    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine(""); //Backpack
        Console.WriteLine((i + 1) + ") " + Bp);
    }

    Console.ReadKey();
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,230
Location
Sydney, Australia
Programming Experience
10+
I don't really know what you're talking about. Are you saying that you want to output to the same line in the console window repeatedly, rather than advancing to the next line? That's my best guess. Please take the time to provide a FULL and CLEAR explanation of the problem. If that takes several sentences or several paragraphs, that's what you should write. Expecting to work out what you want to do from code that doesn't do it is a bit optimistic.
 

porkshopp

Active member
Joined
Apr 13, 2019
Messages
26
Location
Sweden
Programming Experience
Beginner
I mean that every time I call that method, it should be executed, and then the code should be run from the point of execution. Like this:

[Task]

1) option 1
2) option 2
3) option 3
4) backpack
If I choose backpack then it should display the contents in my backpack and then return to the Task in hand
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,230
Location
Sydney, Australia
Programming Experience
10+
I still have no idea what you're talking about. If you call a method then the method executes and then execution continues on the next line after the call. Surely that can't be what you're talking about but I really don't know. If you thought that that qualified as taking the time to provide a FULL and CLEAR explanation, please think again.
 

porkshopp

Active member
Joined
Apr 13, 2019
Messages
26
Location
Sweden
Programming Experience
Beginner
Imagine a console text game. You have different options to choose from. In your backpack, you can use items. For example:
You see an apple:
1) eat apple
2) throw apple
3) stare at apple
4) backpack
if you choose backpack it should display:
Backpack:
1) Bottle
2) String
3) Empty
4) Empty
5) Empty
The backpack should only be for display, so you can't select an item.
You see an apple:
1) eat apple
2) throw apple
3) stare at apple
4) backpack
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,230
Location
Sydney, Australia
Programming Experience
10+
So you're saying that you would display a list of options and, if the user selects this Backpack option, you would display a list of items that are stored in their backpack, then redisplay the previous list of options again?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,230
Location
Sydney, Australia
Programming Experience
10+
How to do that depends on what you're already doing but I would think that the way to go would be to store the current list of options in a list of some sort. You can then have a method that displays the current list of options. You can decide what to do based on the option selected and what to do if the Backpack option is selected is to display the backpack contents, which would be stored in another list, and then call the method that displays the current list of options.
 

porkshopp

Active member
Joined
Apr 13, 2019
Messages
26
Location
Sweden
Programming Experience
Beginner
I have a backpack method in the spoiler above. When I have called it; is there a way to go back to the line of which the code was executed?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,230
Location
Sydney, Australia
Programming Experience
10+
Firstly, the Spoiler tag is for something you want hidden, so don't use it for code you want us to read. Use the Code tag for that.

Secondly, please read what I have already posted. As I have already said, if you want the previous menu redisplayed after the backpack items then you have to call the method that displayed that menu again after calling the method that displays the backpack items. It's not complex.
 

porkshopp

Active member
Joined
Apr 13, 2019
Messages
26
Location
Sweden
Programming Experience
Beginner
Yes I know that, but if I want to reuse the backpack menu, then I can't call a specific question again since it will go back to question 1 when I call backpack from question 5.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,230
Location
Sydney, Australia
Programming Experience
10+
I didn't say to put any extra code in the method that displays the backpack menu. I said that, after you call the method that displays the backpack menu, you call the method that displays the current options menu. They would be two different methods. You call one, then you call the other.

If doing so is not easy, it's because your current code is poorly structured and you should change it. As I said previously, you should have a single method that displays the current options menu, whatever that may be. You can then call that method anywhere you want. That method would get the current options menu from a list somewhere so, whenever the current options change, you simply clear and repopulate that list, then call that same method again. If the user selects the backpack option, you call the method that displays the backpack contents, then call the method that displays the current options menu again.

Of course, if you actually do create that method to display the current options, you actually could call it from inside the method that displays the backpack contents and it would work exactly as expected. I wouldn't do it that way though.
 
Top Bottom