Click here to Skip to main content
11,639,861 members (61,882 online)
Click here to Skip to main content

How to find current ApplicationContext at run time

, 3 Aug 2004 33.8K 2 9
Rate this:
Please Sign up or sign in to vote.
How to find current ApplicationContext 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.

The code

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

    If you need to set the Main Form for ApplicationContext while initializing it, you can add the constructor like this as well:

    public CurrentAppContext(Form AppMainForm)
    {
        this.MainForm = AppMainForm;
    }
  2. Create a public static field of our class of type CurrentAppContext inside of the class.
    public class CurrentAppContext : ApplicationContext
    {
        public static CurrentAppContext CurrentContext;
     
        public CurrentAppContext()
        {}
    
     
        public 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.
    public class CurrentAppContext : ApplicationContext
    {
        public static CurrentAppContext CurrentContext;
     
        public CurrentAppContext()
        {
            CurrentContext = this;
        }
        
        public 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):
    CurrentAppContext _curContext = (new CurrentAppContext()).CurrentContext;

    OOPS! Because we initialize CurrentAppContext() again and point CurrentContext to new CurrentAppContext instance, CurrentContext will be referring to the new instance, but not to our current ApplicationContext. We have to add (If) statement to 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:

public class CurrentAppContext : ApplicationContext
{
    private static CurrentAppContext _currContext;

 
    public CurrentAppContext()
    {
        if (_currContext == null)
        {_currContext = this;}
    }
    
    public 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

Share

About the Author

alphacon
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralUse singleton Pin
April KBR26-Sep-04 16:13
memberApril KBR26-Sep-04 16:13 
GeneralRe: Use singleton Pin
Anonymous22-Nov-04 5:09
sussAnonymous22-Nov-04 5:09 
GeneralRe: Use singleton Pin
c_marius22-Nov-04 5:10
memberc_marius22-Nov-04 5:10 
GeneralRe: Use singleton Pin
sendi_t346-Apr-07 4:41
membersendi_t346-Apr-07 4:41 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 4 Aug 2004
Article Copyright 2004 by alphacon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid