xorpower
xorpower

Reputation: 18973

Executing Output from file other than Program.cs

I had created a new console application in VS 2010.

I had written some code under Program.cs file. Now i created another class and i have written code there. Now when i execute the program from another class, the output is called from program.cs file. How to make project setting so that the output is reflected from another class and NOT program.cs file?

Upvotes: 1

Views: 6636

Answers (4)

Vishal Bhandare
Vishal Bhandare

Reputation: 105

Well i am late for this. But just don't write Main Method in Program.cs and Write it in Other File you are executing. Or else use c# cli compiler to do compile and execution rather than Visual studio

Upvotes: 0

Johnv2020
Johnv2020

Reputation: 2146

Xor power - I'm going to answer your comments as best as possible here

What if i need to call the Main Method of other class and not any user defined method?

A console application can only have 1 main method - this is the entry point for the application.

So to add a new Main method to your program you need to change which class starts first, to do this simply delete the main method from Program.cs and add it to your new class as shown below

class NewClass
{
    static void Main(string[] args)
    {
        Console.WriteLine("hello, world");
    }
}

No that is simple displaying into console: NOT that if i need to output to console from any other class other than Program.cs

On the other hand if you need to get a class to write to the console and it isn't the starting class then you must specify a method and use Console.Writeline as Dave & MrFox have shown above. Examples of this are shown below

class Program
{

    static void Main(string[] args)
    {
        // use a instance of a class to write
        NewClass myNewClass = new NewClass();
        myNewClass.WriteOutPut();

        // use a static class
        NewClass2.WriteOutPut();

        // finally read back so that they we can see what was ouputted
        Console.ReadLine();
    }

}

/// <summary>
/// this is an instance class
/// </summary>
public class NewClass
{
    public void WriteOutPut()
    {
        Console.WriteLine("hello");
    }
}

/// <summary>
/// this is a static class
/// </summary>
public static class NewClass2
{
    public static void WriteOutPut()
    {
        Console.WriteLine("hello");
    }
}

Upvotes: 2

MrFox
MrFox

Reputation: 5116

You need to create objects from the other class in your main method (which normally sits in Program.cs) and call it's methods.

The main method in C# is usually identified with the property "[STAThread]".

So in the main method if your class name is FooBar:

    [STAThread]
    static void Main(string[] args)
    {
        FooBar fooBar = new FooBar();
        fooBar.RunMethod();
    }

Upvotes: 0

Dave White
Dave White

Reputation: 3461

Using the .NET runtime, any console application will have a Main() function. This is the entry point of the application that is executed by the runtime to get your application running. This is probably located in Program.cs for you right now.

In order to output text to the console window from ANY class in your console application, you just need to put:

Console.WriteLine("Some message")

or

Console.Write("Some message without a linefeed after it")

to output to the console window of your application.

In order to read input from the console in ANY class in your console application, you'll use

Console.ReadLine(**variable to take in input**);

or

Console.Read(**variable to take in input**);

Here is a link to the complete API for the Console class in .NET 4

I hope this gets you going.

Upvotes: 0

Related Questions