none
Please help me understand what C# solution code should go in console and what should go in forms. RRS feed

  • 問題

  • I understand most of the basic C# language and can write simple programs either in console or forms. I'm not at all clear on why I should put anything in console at all. I need to break down a 1000-line C# program into smaller pieces. So far I've been able to add classes to a project and add classes to a library. How to mix console and forms apps in one solution is giving me problems. Thanks for any help. -Ron2854
    2019年12月14日 下午 03:55

所有回覆

  • It's pretty simple:

    What kind of user interaction do you need? What tell the requirements about automation capabilities?

    Just think about the consequences, when you need console and forms. This means that you need shared logic. anything shared should go into libraries. Depending on your architecture, anything which is not UI, either forms or console, should go into one or more libraries.

    2019年12月14日 下午 04:48
  • Hi Ron,
     
    If your application is entirely contained on a Form, you don't need a console at all. When creating a new application, you'd start with "File | New | Project" and choose "Windows Form App". Visual Studio automatically sets the new WindowsForms as the "Startup Project". No Console Project is necessary (and one is NOT created).
     
    For a simple application, you could put everything into that one Project (which means you'd end up with one EXE for your whole application).
     
    For more complex applications, you might want to separate out some of your functionality into other libraries, as Stefan mentioned. These "other libraries" are simply other Projects, which you add to your solution with "Add New Project", and choose "Class Library". These will end up compiling as DLLs (and then your application would consist of one EXE and one or more DLLs).
     
    Hope this helps ...  =0)

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    2019年12月15日 上午 01:25
  • Why are you asking about console applications? If you can explain why then we can help you but if you do not know why you are asking about console applications then you do not not need a console application.


    Sam Hobbs
    SimpleSamples.Info

    2019年12月15日 下午 06:13
  • Greetings Ron.

    Speaking for myself, the only time I use console applications is to write simple little test apps.

    For example, if a poster on these forums asks, "I'm trying to apply *some formula* to two numbers but I get the wrong answer. What am I doing wrong?" , I will take their code and put it in a console app with the two numbers hard-coded and the result output via Console.WriteLine. That's because I can write such a small app a lot faster than creating a form, adding input fields, adding a button to compute the result, adding a field to display the output, and so on.

    But for a more complicated application that will be used by other people, a console app looks too unprofessional.

    2019年12月16日 上午 04:46
  • Just a sample where it makes imho always sense:

    Anything like API querying (SOAP, REST etc.) goes into a separate library and gets always a console to use it, even when the customer doesn't need it in the first iteration of requirement gathering. In the long term such tasks tend to be automated (by using an console application and task scheduler or a Windows service) or you need a tool for testing API availability. And such APIs my change faster than your application, thus abstracting them to their own layer makes sense.

    2019年12月16日 上午 10:20