Call Main from another method

Jacob

Is there a way to call Main() from another method "manually"? I have the following code:

static void Main(string[] args) {
    # some code
    function();
}

static void function() {
    #some code
    Main(); # Start again
}

I have for example simple console calculator, when I calculate and print the result in function(), I want to start again with e.g. "Enter two numbers :" in Main() method.

MetaColon

You have to add the parameter as well. If you don't use the parameter in your main functin you have to possibilities:

  1. Give null as parameter
  2. Make the parameter optional

null as parameter

This would work like that:

static void code()
{
    Main(null);
}

Optional property

Then you'd have to modify the parameter like that:

static void Main (string[] args = null)
//...

You can't delete the parameter in the Main function because it is called by some other stuff, you don't want to modify.

If you do use the args parameter in the main function, null might not be a good idea, then you should replace it by something like new string[0]:

static void code()
{
    Main(new string[0]);
}

However, this isn't valid as optional parameter because optional parameters have to be compile-time constant.

If you use it with null you could get a NullReference exception if you use it without checking the value for null before. This can be done by two ways:

  1. Using an if condition
  2. Null propagation

An if condition would look like this:

static void Main (string[] args = null)
{
    Console.Write("Do something with the arguments. The first item is: ");
    if(args != null)
    {
        Console.WriteLine(args.FirstOrDefault());
    }
    else
    {
        Console.WriteLine("unknown");
    }

    code();
}

Null propagation like this:

static void Main(string[] args = null)
{
    Console.WriteLine("Do something with the arguments. The first item is: " + (args?.FirstOrDefault() ?? "unknown"));

    code();
}

By the way, you forgot a semicolon after your Main() call.


Maybe you should rethink your code design anyway, as you call the code method inside the main method and the main method inside the code method, which may result in an endless loop and therefore in a StackOverflow exception. You could consider to put the code you want to execute from the code method in another method which you'd then call inside the main method and inside the code method:

static void Initialize()
{
    //Do the stuff you want to have in both main and code
}

static void Main (string[] args)
{
    Initialize();
    code();
}

static void code()
{
    if (condition /*you said there'd be some if statement*/)
        Initialize();
}

Here you can get more information about methods. But as this is a problem which normally occurrs at the beginning of learning how to code, you should probably go through a tutorial like this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call from a main of one class, a main method of another class

From Dev

How do you call a scanner variable from main into another method?

From Dev

Why not call the static method "main" of a class from another class?

From Dev

How can i call the function from main event into another method?

From Dev

How can I run testNG through a call from another method, let's say a main method?

From Dev

How to call toString() Method from one class to another class without main method in java

From Dev

How to call another function in the "static main" method?

From Java

calling another method from the main method in java

From Java

Call a method from another JFrame

From Dev

Method call from another form

From Dev

How to call an individual method from another python module which has got a main function in it

From Dev

How to call a method that is defined in the main class('_MyHomePageState()') from a onPressed of a Flat Button defined in a another class?

From Dev

I am not able to call method from another file to main project file to run my test case

From Dev

How do I call a method inside another class from my main?

From Dev

Is it possible to call to main method from a class that already has a main method?

From Dev

Is it wrong to call main from another function in python

From Dev

Call function in main thread from another thread?

From Dev

Call method from Main Activity into Fragment Adapter

From Dev

How to call a constructor from main method in java?

From Dev

JavaFX call a controller method from the main class

From Dev

How to call a class from Main() method?

From Dev

Android Call a method from another method

From Dev

Is there a way to call a class method from another method?

From Dev

How to call a method from within another method?

From Dev

How to call a method from an another method with ruby?

From Dev

Call a JNI method from another JNI method

From Dev

Calling a class from another class with main method

From Dev

How to call a component's method from the main method?

From Java

Java: how to call non static method from main method?

Related Related

  1. 1

    Call from a main of one class, a main method of another class

  2. 2

    How do you call a scanner variable from main into another method?

  3. 3

    Why not call the static method "main" of a class from another class?

  4. 4

    How can i call the function from main event into another method?

  5. 5

    How can I run testNG through a call from another method, let's say a main method?

  6. 6

    How to call toString() Method from one class to another class without main method in java

  7. 7

    How to call another function in the "static main" method?

  8. 8

    calling another method from the main method in java

  9. 9

    Call a method from another JFrame

  10. 10

    Method call from another form

  11. 11

    How to call an individual method from another python module which has got a main function in it

  12. 12

    How to call a method that is defined in the main class('_MyHomePageState()') from a onPressed of a Flat Button defined in a another class?

  13. 13

    I am not able to call method from another file to main project file to run my test case

  14. 14

    How do I call a method inside another class from my main?

  15. 15

    Is it possible to call to main method from a class that already has a main method?

  16. 16

    Is it wrong to call main from another function in python

  17. 17

    Call function in main thread from another thread?

  18. 18

    Call method from Main Activity into Fragment Adapter

  19. 19

    How to call a constructor from main method in java?

  20. 20

    JavaFX call a controller method from the main class

  21. 21

    How to call a class from Main() method?

  22. 22

    Android Call a method from another method

  23. 23

    Is there a way to call a class method from another method?

  24. 24

    How to call a method from within another method?

  25. 25

    How to call a method from an another method with ruby?

  26. 26

    Call a JNI method from another JNI method

  27. 27

    Calling a class from another class with main method

  28. 28

    How to call a component's method from the main method?

  29. 29

    Java: how to call non static method from main method?

HotTag

Archive