0

I was making a debug menu, and I was wondering if there was a way to take user input and run that as a line of code while the application is running.

here's some code to help you understand what I mean:

using System;
using System.Threading;

namespace ExampleCode
{
    class Program
    {
        public static void debugMenu()
        {
            string codeToRun = Console.ReadLine();
            //execute codeToRun as a line of code
        }
    }
}

so if the input was "debugMenu();" then it would rerun the method.

it would help a lot with debugging, because I could reach and test, any method while the application is still running.

Thank you for the answers!

3DartBlade
  • 71
  • 7

2 Answers2

0

First of all, you are having some mistakes here, the entry point of any program is the main method which i cant see anywhere here. Now to answer your question, there is a way but not exactly what you are thinking. You should have a switch statement something like this

static void Main(string[] args)
{           
    string userInput = Console.ReadLine();
    switch(userInput)
    {
        case "debugMenu()":
            debugMenu();
        break;
        case "whatever":
            anotherFunction();
        break;
    }
}

public static void debugMenu()
{
    //whatever you want this function to do
}

public static void anotherFunction()
{
    //whatever you want this function to do
}

Personally i would not recommend this, but it is fully functional the string and the function itself doesnt matter. There are other ways but it involves reflection and since you re struggling with basics, i wouldnt want to confuse you further

Edit: Further more, your idea isnt a good practise for debugging, if you need to test the functionality of a function i would recommend you to search how to do Unit Test, and they are also debuggable

nalnpir
  • 1,167
  • 6
  • 14
  • I would do the same thing if I didn't find another solution, but it would be a bit too many for around 20 methods which I have as of now, and if I made other methods, I'd have to add another case for that, that's why I scrapped the idea. Thank you anyways! – 3DartBlade Apr 05 '20 at 17:31
0

If all you want to do is call methods whilst debugging then you can use Visual Studio's Immediate Window which allows you to dynamically execute C# code in the debugger. This would allow you to call into another method whilst debugging without having to change your code.

Additionally note that Visual Studio allows you to drag the instruction pointer around whilst debugging to alter the program flow.

Edit: it sounds to me like you need to familiarise yourself with unit testing.

simon-pearson
  • 1,601
  • 8
  • 10
  • Thank you, but I need to test that if I run specific methods after each other, do they cause bugs or not. I can only use that if the application isn't running. – 3DartBlade Apr 05 '20 at 17:37