user310291
user310291

Reputation: 38180

How to hide the plumbings of program.cs

In a typical winform program.cs contains the entry point:

     static void Main(string[] args) {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        // the code goes on

I want to create my own framework which hide the plumbings of all stuffs that has nothing to do with application domain so I'd like to be able to create a base class in an another namespace something like this

namespace MyFramework
{
    public partial class Program
    {

        

        [STAThread]
        static void Main(string[] args)
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
            
            // the code goes on

And then in my app namespace program.cs inherit from the one above so that I can get rid off the Application.EnableVisualStyles and write only something like this

        static void Main(string[] args): base()
        {
           //only app domain code

I can inherit from base class if I remove static attrib from both class but I am not able to remove static from main entry method so I cannot really finalize.

I don't want to use code behind to hide the plumbings because I need to port this to other languages that doesn't have code behind.

So is there a way to achieve this simple and legitimate goal with OOP (inheritance or not, static or not) only without using some big external framework ?

For example is there a way I could delegate main entry point to another class in the framework namespace ?

Upvotes: 0

Views: 543

Answers (3)

Yogesh
Yogesh

Reputation: 14608

The best way to do this would be to make your Program class a non-static class which derives from another class (the class which will do all the work you need). You can declare a static method in the base class and call that method before Application.EnableVisualStyles();.

The code for the same will be like this:

public class Program : MyBaseClass
{
    [STAThread]
    static void Main()
    {
        MethodToDoAllTheHardWork();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

public class MyBaseClass
{
    public static void MethodToDoAllTheHardWork()
    {
    }
}

The reason for declaring a static method in MyBaseClass is because the Main method has to be a static method and you can only call the static members of the same or base class.

Upvotes: 1

VinayC
VinayC

Reputation: 49165

The entry point method needs to be static so using inheritance (as perceived by you) would not work but there can be several ways to do what you want to do. For example,

namespace MyFramework
{
    public static class AppStarter
    {

        static EventHandler AppStarting;

        [STAThread]
        static void Run(string[] args)
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            // the code goes on

            if (null != AppStarting)
            {
               AppStarting(null, EventArgs.Empty);
            }

Now, you can use it

 public partial class Program
 {
        [STAThread]
        static void Main(string[] args)
        {

            AppStarter.AppStarting += new EventHandler(MyStartupCode);
            AppStarter.Run(args);

Yet another inheritance based variation would be

namespace MyFramework
{
    public abstract class Application
    {

        protected abstract void OnBeforeInit();
        protected abstract void OnAfterInit();

        protected void Run(string[] args)
        {
            OnBeforeInit();

            // Do initialization

            OnAfterInit(); 

            ...
        }

And you will use it as follows:

 public class MyApplication: MyFramework.Application
 {
        [STAThread]
        static void Main(string[] args)
        {

            (new MyApplication()).Run(args);

Upvotes: 1

leppie
leppie

Reputation: 117220

Your entrypoint needs to be static.

Upvotes: 1

Related Questions