Re: Console Application

"R. Rogers" <[email protected]> Tue, 11 Dec 2007 16:57:53 -0500
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <001801c83c40$e19ac810$6800a8c0@Speedster>
Hi Ron,

Your approach is the approach I'll be taking.

Chris does raise valid points: why bother with try/catch or 'using'.
A lot of times intuition/"it feels right" influences our decision,
especially if there is no perceived disadvantage. Intuitively, I would think
that I am programming more defensively using the approach you've outlined.

Thanks guys,

Richard
-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps and
controls [mailto:[email protected]] On Behalf Of Ron Young
Sent: December 11, 2007 1:32 PM
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Console Application

I've seen it done both ways, where an instance of MyConsoleApp is newe'd up
and executed from Main(). Or where all the program code is driven from
Main() and static methods.

You could separate your program code outside of the default Program class.
It might make it clear if you rename that default Program class to
ProgramDriver.

Then your MyConsoleApp class can implement IDisposable, and you'd have this:

class ProgramDriver
{
   static void Main()
   {
          using (MyConsoleApp app = new MyConsoleApp())
          {
                app.Run();
           }
    }
}

class MyConsoleApp : IDisposable
{
     ...
}

I don't use console apps much except to dump output of some prototype code,
or as a service host of some sort. Usually when my code file gets cluttered
with static methods, and things stop making sense, I'll break it out into a
separate class.

Ron

________________________________________
From: Discussion forum for developers using Windows Forms to build apps and
controls [[email protected]] On Behalf Of R. Rogers
[[email protected]]
Sent: Tuesday, December 11, 2007 12:29 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]