Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you call a method from static main()?

Tags:

c#

.net

I have a console application with a Main method and a function.

How can I make a function call from the Main method?

I know the code below won't work

static void Main(string[] args)
{            
   string btchid = GetCommandLine();// GetCommandline is a mthod which returns a string
}
like image 808
Priyanka Avatar asked Apr 20 '11 09:04

Priyanka


People also ask

How do you call a method in static Main?

To call a static method from another class, you use the name of the class followed by the method name, like this: ClassName. methodName().

Can you call a method from Main?

Once a method is declared in a class it can be called in the main or any other method. There are also some built-in methods already defined in Java libraries. To call any built-in or self-defined methods using the syntax described in detail below.

How do you call a method in main method?

Call a Method Inside main , call the myMethod() method: public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); } } // Outputs "I just got executed!"

Can we call static method inside main method?

Static method of a class can be called by using the class name only without creating an object of a class. The main() method in Java must be declared public, static and void. If any of these are missing, the Java program will compile but a runtime error will be thrown.


2 Answers

There's also

var p = new Program();
string btchid = p.GetCommandLine();
like image 164
Ray Avatar answered Sep 18 '22 15:09

Ray


Make the GetCommandLine static!

namespace Lab
{
    public static class Program
    {
        static string GetCommandLine()
        {
            return "Hellow World!";
        }

        static void Main(string[] args)
        {
            System.Console.WriteLine(GetCommandLine());
            System.Console.ReadKey();
        }
    }
}
like image 45
Kees C. Bakker Avatar answered Sep 19 '22 15:09

Kees C. Bakker