0

I have started a visual studio project for a console application for C#.

I have started the project and usually save my progress as I refine my program.

I'll typically save the cs files like say FILE1a.cs, FILE1b.cs and FILE1c.cs.

The thing is, when I press F5 to debug the program, visual studio always defaults to debugging say, FILE1a.cs

How do get visual studio to debug or run the other cs files?

2
  • 1
    Try using GIT or another versioning system will solve the underlying problem. Commented Jul 23, 2016 at 7:21
  • If I remember correctly, use the configuration manager, then application tab, then change the startup object. Commented Jul 23, 2016 at 7:24

3 Answers 3

3

That's what version control systems are for, otherwise you'll end up with "FILE1-final.cs", "FILE1-final2.cs", "FILE1-reallyfinal.cs" and so on. Keep your code in one file, and use a system that records the editing history of that file.

Currently Git is hip and happening, alternatives are Mercurial and Team Foundation Server.

That being said, you can have only one startup method in a project, and it is cumbersome to change this method every time you want to test your code.

If you're experimenting with different implementations of a class, then simply instantiate the appropriate class.

You can let these implementations implement an interface so you can refer to them through one variable:

public class Program
{
    public static void Main()
    {
        IImplementation implementation = new Impl1();
        //IImplementation implementation = new Impl2();
        //IImplementation implementation = new Impl3();

        Console.WriteLine(implementation.Foo());
    }
}

public interface IImplementation
{
    string Foo();
}

public class Impl1 : IImplementation
{
    public string Foo()
    {
        return "Foo";
    }
}

public class Impl2 : IImplementation
{
    public string Foo()
    {
        return "F" + "o" + "o";
    }
}

public class Impl3 : IImplementation
{
    public string Foo()
    {
        return string.Concat("F", "o", "o");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

If you want to just do this during debugging then before pressing "F5"; Right click on required file in "Solution Explorer" and then set as Startup.

But if you want the same thing while running then this depends on how you are calling the Exe. One way is to pass a argument to EXE this is captured in "args" array that is passed as parameter to "static void main". Inside the "main" you can execute the specific file based on the argument passed.

Hope this helps

Comments

3

In your console application project, open Properties tab, then Application tab, and there you have "Startup project" dropdown. Select the file you want and run. Keep in mind only classes with an entry point can be run that way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.