Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#
Article

How to get current ApplicationContext instance at run time

Rate me:
Please Sign up or sign in to vote.
3.47/5 (6 votes)
3 Aug 20041 min read 54.4K   9   4
Step by step instruction how to get current ApplicationContext instance at run time

Introduction

While I was working on one of my projects, I got into a situation where I needed to get the current ApplicationContext instance from one part of my software. I started my research on MSDN, then on the Internet. As some of you probably already know by reading this article, I did not find the results I was looking for. I cannot understand why there is no easy way to get the current ApplicationContext instance with regular MS defined properties or methods. I have, however, created a simple solution.

  1. Create a new class inherited from ApplicationContext.
    C#
    public class CurrentAppContext : ApplicationContext
    {
        CurrentAppContext()
        {
        }
    }

    If you need to set the Main-Form for ApplicationContext while initializing, you can add the constructor like that as well.

    C#
    CurrentAppContext(Form AppMainForm)
    {
        this.MainForm = AppMainForm;
    }
  2. Create a public static field pointed to our CurrentAppContext form inside the class.
    C#
    public class CurrentAppContext : ApplicationContext
    {
      public static CurrentAppContext CurrentContext;
    
      CurrentAppContext()
      {
      }
    
      CurrentAppContext(Form AppMainForm)
      {
          this.MainForm = AppMainForm;
      }
    }
  3. Because static class fields exist only once and are not created again, when we create a new class instance, we can initialize our field from the constructor.
    C#
    public class CurrentAppContext : ApplicationContext
    {
      public static CurrentAppContext CurrentContext;
    
      CurrentAppContext()
      {
        CurrentContext = this;
      }
    
      CurrentAppContext(Form AppMainForm) : this()
      {
        this.MainForm = AppMainForm;
      }
    }
  4. If you need to get the ApplicationContext at run time, we will create a new instance of CurrentAppContext and get CurrentContext, which is exactly what we need. (See below.)
    C#
    CurrentAppContext _curContext = (new CurrentAppContext()).CurrentContext;

OOPS!  Because we initialize CurrentAppContext() again and point CurrentContext to new CurrentAppContext instance, CurrentContext will be referring to new instance, but not to our current ApplicationContext. We have to add If statement to the constructor and make sure that if the CurrentContext is not null we do not make a new assignment. Also, let’s convert public field to Read Only property.

Final class will look something like this: (See below)

C#
public class CurrentAppContext : ApplicationContext
{
  private static CurrentAppContext _currContext;

  CurrentAppContext()
  {
    if (_currContext == null)
    {
      _currContext = this;
    }
  }

  CurrentAppContext(Form AppMainForm) : this()
  {
    this.MainForm = AppMainForm;
  }

  public CurrentAppContext  CurrentContext
  {
    get
    {
      return currContext;
    }
  }
}

Now you can create it inside Main() as usual and get it from any part of your software. If necessary, you can extend it with new fields and methods.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs calling CurrentAppContext.CurrentConte better? Pin
Member 1227809928-Dec-17 10:24
Member 1227809928-Dec-17 10:24 
NewsUse My?? Pin
Member 125283288-Mar-17 2:03
Member 125283288-Mar-17 2:03 
GeneralThank you! Pin
voiger25-Jul-08 1:23
voiger25-Jul-08 1:23 
QuestionBut why? Pin
Labrat0024-Aug-04 12:37
Labrat0024-Aug-04 12:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.