1

I m a newbie.

I have 2 .cs files in my solution/Project. I want to run 1 of the file individually to see if it is working fine. how can I do it?

following is my project structure.

enter image description here

I have to run either OpenBrowser.cs or ReadExcel.cs. Don't want to write the main method in each of them and do the commenting stuff. Is there away?

enter image description here

vaibhav Khade
  • 35
  • 1
  • 8
  • 3
    You can create one Program.cs file and write a main method in it. The main method should call method of those classes. The you can pass arguments to main method from command line and based on the value of the argument you can call method from appropriate class. – Chetan Nov 27 '19 at 08:11
  • 1
    I can't see the image that you attached. What do you mean by running the .cs files? You never "run" a .cs file. It is a part of an executable output (aka .exe). Re-upload the image from another source and maybe I can help. – ycansener Nov 27 '19 at 08:12
  • 1
    I opened my VPN to see the image (govermental issues unfortunately...) What is the type of your project? It looks like this is a console application but you rename the Program.cs file? If this project have two seperate functionality, best practice is seperating them into two different project and if the reason behind merging that two functionality in a single project is reusing the components, then you can create a class library to share those components between two console app projects. To run two different console applications with a single click, you can use bat files or ps1 scripts. – ycansener Nov 27 '19 at 08:21
  • 2
    Write unit tests 'to see if it is working fine' ;) – nilsK Nov 27 '19 at 09:10

1 Answers1

0

No, actually, you cannot run Files, you can only run projects, and also only if they are marked as a Program - not as Library.

You cannot run a file, cause there is no definite starting point to it. The Compiler or Debugger would not know what function to call, inside your file. That is why there is this fixed name "Main" to commit this to the Compiler.

The only exceptions are Unit Tests. If you mark a Method in a [TestClass] as [TestMethod] you are able to run it Standalone. But this is per Method, not per File - again.

Holger
  • 2,446
  • 1
  • 14
  • 13
  • Using the compiler services, you actually *can* run a file: https://stackoverflow.com/a/4181855/6401643. If it has a valid entry point. – Prophet Lamb Nov 27 '19 at 10:30
  • 1
    @RenéCarannante That's what I said. You cannot run it, cause there is no entry point. Create one and you can. Creating a second Program, compiling an external file at runtime, is possible, but quiet a lot of work. Having multiple possible entrypoints and changing it in the project settings (From Main to something else) might be a few magnitudes simpler. Now we have two solutions almost without any programming. For Learners and quick experiments the TestMethod comes quiet handy, although it's not really intended for this use. Choice depends on if it's for the programmer itself, or for a user. – Holger Nov 27 '19 at 11:45
  • Aye sir, I just wanted to point to the other post, because some stray people might get the wrong idea about the capabilities of .NET here. While it certainly isn't a piece of cake to compile and run external files it also isn't too hard. The problems arise, when you want to do something useful with them, like extension scripts. For such uses setting up the environment is very hard indeed. – Prophet Lamb Nov 30 '19 at 19:35
  • @RenéCarannante Sure it's not hard to compile a file. A single file project is the most simple thing in the world. The question related more to comfort, to do it with a few clicks only and starting with a multi-file project. Splitting a project into smaller projects is not everyday business, it's nothing else we are doing here. Each compilation ends up in an assembly, and each assembly has only exactly one Entry point. For anything else you need a Framework around it...you can take the Test-Framework, or program something yourself. – Holger Dec 01 '19 at 10:12