Question:
C# ==> Console Application ?!?!??!?
aryaxt
2008-08-20 21:17:36 UTC
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)??
Five answers:
Me M
2008-08-20 22:06:37 UTC
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.
Berkut
2008-08-20 21:39:14 UTC
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?
valentina
2016-05-30 08:58:42 UTC
Software is everywhere. Some of it is in your face graphical, like video games. Some software is hidden away, like in the engine management system of your car. Some is in between, like the software running at Yahoo! that delivered this page to you. Console applications are typically application that just get on and do their job. The C++ compiler, for instance, is a console application. Depending on the IDE you use there will be a view somewhere that you can see the compiler introducing itself, telling you what it's doing and spitting out complaints about the errors in your program. The IDE will then map the errors from the console application into the original text. I'm a professional software developer working near the bleeding edge of technology and I regularly write both GUI and Console based software, both with C++ and more modern languages. This week my time has been pretty evenly divided by three pieces of software, one with a GUI, one a console application and another that is embedded within some other software and so only has a binary interface.
anonymous
2008-08-20 21:26:42 UTC
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.
Alex M
2008-08-22 20:03:14 UTC
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/csharplanguage/thread/fa9ee695-9632-4a86-8c48-b42dbe5be397/



but give it a look.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...