How to get the Application object of the current executable

Locked How to get the Application object of the current executable

  • Thursday, May 05, 2005 2:50 AM
     
     
    Hi All,

    I am developing a Windows application using C#.  I want to store a common variable in either the main application (Program.cs) or the main form of the application and be able to pass that variable to any control or module in the program.

    Is the a way to get the application object, or the main form object of a Windows application in C#?

    Thanks in advance.

    David

All Replies

  • Thursday, May 05, 2005 8:48 AM
     
     Answered

    Hi David,

    I think a clean design approach would be to have these as static properties of a separate class. Example below shows a class GlobalAppData with a Sample static property DataSample1. This property can be accessed from throughout your application.
    --------------------------------------------------
    Example Class: GlobalAppData
    --------------------------------------------------
    public class GlobalAppData
    {
        private static string strDataSample1 = "";

        public static string DataSample1
        {
                get
                {
                    return (strDataSample1);
                }
                set
                {
                    strDataSample1 = value;
                }
        }
    }

    --------------------------------------------------
    Example Class: Usage in a Form_Load
    --------------------------------------------------
    private void Form1_Load(object sender, EventArgs e)
    {
          GlobalAppData.DataSample1 = "My Value";
    }
    --------------------------------------------------

    Regards,
    Vikram

  • Monday, May 09, 2005 4:24 AM
     
     
    Yes, it is a clean approach.  However, my question is "how to set it once but read it many times from different components within an application"

    Using your example, where would you put the GlobalAppData class?  You have to declare a instance of it inside another class, right?  Let's say that you declare an instance of it in a main form:

    public  class frmMain : Form
    {
       GlobalAppData appData = new GlobalAppData();

       
    }

    And how do you get that appData instance from another form, user control within the same application?

  • Monday, May 09, 2005 5:22 PM
     
     

    You would not create an instance of GlobalAppData since all it's members are static.  Instead you access the static property like so... GlobalAppData.DataSample1 (from anywhere in your code).  As far as initializing the data, if it's only set once then GlobalAppData should initialize it itself (if possible).



    public class GlobalAppData
    {
       private static string dataSample1 = null;

       public static string DataSample1
       {
          get
          {
             if( dataSample1 == null)
             {
                TODO: Initialize dataSample1 here...
             }
             
             return dataSample1;
          }
       }
    }

     

  • Monday, May 09, 2005 9:08 PM
     
     
    Very interesting...

    I may sound very stupid, but I never try this before... (will try this tonight after I got home)

    So can you have this class in its very own separate file and have the file included in your solution?  Or you have to have this class in the same file where your static main() function is.

    Thanks.

    DQ
  • Tuesday, May 10, 2005 2:33 PM
     
     Answered

    Hi,

    There are two options. Please do some reading on the concept of a Namespace in case you are not aware of it.

    Namespace:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfnamespace.asp

    Ok, after you have read that...the two options are:
    1. You create the Global class as a separate class outside of any class within the same Namespace as ur other classes. In this case, u can directly use the Class.Method() within any ur classes directly.

    2. You create the Global Class as a separate class outside if any class within a separate namespace. In this case, u need to add a using clause to import the namespace and then use the class within the other classes.

    Regards,
    Vikram

  • Wednesday, May 11, 2005 5:04 PM
     
     

    Vikram,

    Thanks...