Re: Console Application
Mark Brackett <[email protected]> Wed, 12 Dec 2007 12:09:15 -0500
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <[email protected]> |
1. Depending on purpose/complexity, I'll do either. If it's an actual
app (vs prototype or something) then I'll usually create a separate
class with most of the program code. Mainly, I do that to separate the
program logic from the UI.
Otherwise, I'll just throw static methods in Program until it starts to
get uncomfortably large (or I actually need instance variables) - at
which point I'll randomly add classes to the bottom of the Program.cs
file until I get the urge to cleanup. That makes for quick prototyping,
and easy scripting with CS-Script - but can get unwieldy pretty quick.
2. Unless you're disposing of something, there's no reason to implement
IDisposable, or wrap into a using{} block. The finally{} code you have
is superfluous as well - you're just about to exit the app, you really
don't need to clean up after yourself.
Given your initial code (which is obviously an example, but I'll run
with it), it actually looks like you should have Initialize() and
Processing() throw exceptions instead of return false for an error, then
move your Completion() code into an IDisposable.Dispose(), which gives
you a reason to use using{} again.
HTH.
--MB
> -----Original Message-----
> From: Discussion forum for developers using Windows Forms to build
apps
> and controls [mailto:[email protected]] On Behalf Of
> R. Rogers
> Sent: Tuesday, December 11, 2007 1:30 PM
> To: [email protected]
> Subject: [DOTNET-WINFORMS] Console Application
>
> Hi,
>
> As far as I know, there's no forum for Console apps, so I'm placing my
> questions here.
>
> I'm just writing a simple console app:
>
> namespace HierarchyTest
> {
> class MyConsoleApp
> {
> private bool Initialize()
> {
> bool Result = false;
>
> return (Result);
> }
>
> private bool MainProcessing()
> {
> bool Result = false;
>
> return (Result);
> }
>
> private void Completion()
> {
> }
>
> static void Main(string[] args)
> {
> }
> }
>
> I have two questions:
> 1. Main is declared as static. As a result, I can't directly call
> MyConsoleApp.Initialize() from within Main. I can take the approach of
> either creating my methods as static, or writing something like this
in
> main:
>
> try
> {
> MyApp = new MyConsoleApp();
> if (MyApp.Initialize())
> if (MyApp.Processing())
> {
> MyApp.ProcessingMessage =
> String.Format(_ProcessingComplete,
> MyApp.FoldersProcessed, MyApp.FilesProcessed);
> Console.WriteLine(MyApp.ProcessingMessage);
> }
> MyApp.Completion();
> }
> finally
> {
> if (MyApp != null)
> MyApp = null;
> }
>
> Which approach do you take in your console apps, and why?
>
> 2. I'd like to use a 'using' instead of a try/finally block, but MyApp
> doesn't implement IDisposable, hence the following code won't compile:
>
> using (MyApp = new MyConsoleApp())
> {
> if (MyApp.Initialize())
> if (MyApp.MainProcessing())
> {
> MyApp.ProcessingMessage =
> String.Format(_ProcessingComplete,
> MyApp.FoldersProcessed, MyApp.FilesProcessed);
> Console.WriteLine(MyApp.ProcessingMessage);
> }
> MyApp.Completion();
> }
>
> What would you do? Would you declare MyConoleApp this way?
> class MyConsoleApp : System.Idisposable
>
> Thanks for any/all feedback.
>
> Richard Rogers
> http://www.RichardRogers.ca/
> (MSN) [email protected]