Question:

C# ==> Console Application ?!?!??!?

by  |  earlier

0 LIKES UnLike

I always created windows applications.

but now i need to create a console application.

class Server

{

static void Main(string[] args)

{

}

public void asd()

{

}

}

the function asd is public and it's inside the whole class.

So why can't I access it inside static void Main(string[] args)??

 Tags:

   Report

4 ANSWERS


  1. Static methods and variables are handled differently from non-static variables/methods. They are loaded into a different part of the memory and they can be accessed without creating an object. Your asd() method is not static. Therefore, to access it, you need to create an object. That method is in your Server class, so you need to change your code to something like this:

    class Server

    {

       public static void Main(string[] args)

       {

          Server myServer = new Server();

          //call asd function

          myServer.asd();

       }

       public void asd()

       {

          //code for asd function

       }

    }

    I suggest you read up on how static methods are different from regular methods, because they can be kind of confusing at first.  


  2. Fairly simple rule for any beginning C or Java programmer:

    - never call "main" function from inside your source code

    Ok here is what happens when you start any application no matter if it is console, windows, linux and etc..:

    - OS always looks for "main" function and always calls it before any other function in the application, so it is the main function which calls all other functions, but you never should call main function from any other function in your application (although there are few exceptions to this rule).

    I think you should study more about Object Oriented Programming including:

    - difference between class and object, which methods and variables are inherited from class to objects, and which are not

    - difference between static and non-static functions and variables

    - why main method always have to be static and public and why it always have array of string as parameter

    But I am still curious what was your reason for calling main function from asd?

  3. An example is (with treading):

    class Program

        {

            static void Main(string[] args)

            {

                Program obj = new Program();

                Thread t = new Thread(obj.WriteY);

                t.Start();

                while (true) Console.Write("X");

            }

            private void WriteY()

            {

                while (true)

                    Console.Write("Y");

            }

        }

    Its not exactly what you want.

    http://forums.msdn.microsoft.com/en-US/c...

    but give it a look.

  4. you can, but I think I know what your real problem is.

    you're trying to compile it and it's complaining that asd() isn't static; you have 2 choices, declare asd as a static public/protected/private void OR, create a brand new class and instantiate it inside main()

    I heartily recommend going down the new class route, using all static functions is very ugly.

    no, you cannot make main() non-static.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions