Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I have a class globally accessible throughout an entire project/application

How do I have a class globally accessible throughout an entire project/application

Scheduled Pinned Locked Moved C#
cssdockerhelpquestionworkspace
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tkrn
    wrote on last edited by
    #1

    I am trying to create a Log Console that will be a hidden form that will called up when LogConsole.Show() is issued, and write to the Data Grid that is not bound by using the methods as defined. I never done anything like that and I need direction. Take a look at the code below to get a feel for what i am trying to do.

    namespace System.Diagnostics
    {
    public class LogConsole
    {
    public static readonly LogConsole Instance = new LogConsole();
    private LogConsoleWrapper LogConsoleForm = new LogConsoleWrapper();

        public void Show()
        {
            LogConsoleForm.Show();
        }
    
        public void Hide()
        {
            LogConsoleForm.Hide();
        }
    
        public void Focus()
        {
            LogConsoleForm.Focus();
        }
    
        public void WriteLine(string message)
        {
            LogConsoleForm.Write(message);
        }
    
        public void WriteLine(string message, bool error)
        {
            LogConsoleForm.Write(message, error);
        }
    
        public void WriteLine(string message, Exception ex)
        {
            LogConsoleForm.Write(message, (Exception)ex);
        }
    }
    
    public class LogConsoleWrapper : System.Windows.Forms.Form
    {
        /// /// Required designer variable.
    	/// 
    	/// 
    	private System.ComponentModel.Container components = null;
    
        static string sSource = "PIMSLite2008 Log Console";
        static string sLog = "Application";
    
        public LogConsoleWrapper()
    	{
    		InitializeComponent();
    
            if (!EventLog.SourceExists(sSource))
                EventLog.CreateEventSource(sSource, sLog);
    
            clock.Start();
            lblUser.Text = System.Environment.UserName.ToString().ToUpper();
    	}
    
        /// /// Clean up any resources being used.
        /// 
        /// 
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    
        #region User Defined Code
    
        private void clock\_Tick(object sender, EventArgs e)
        {
            lblDate.Text = DateTime.Now.ToShortDateString();
            lblTime.Text = DateTime.Now.ToLongTimeString();
        }
    
        public void Write(string message)
        {
            string\[\] tmp = new string\[3\];
    
            tmp\[0\] =
    
    B M 2 Replies Last reply
    0
    • T tkrn

      I am trying to create a Log Console that will be a hidden form that will called up when LogConsole.Show() is issued, and write to the Data Grid that is not bound by using the methods as defined. I never done anything like that and I need direction. Take a look at the code below to get a feel for what i am trying to do.

      namespace System.Diagnostics
      {
      public class LogConsole
      {
      public static readonly LogConsole Instance = new LogConsole();
      private LogConsoleWrapper LogConsoleForm = new LogConsoleWrapper();

          public void Show()
          {
              LogConsoleForm.Show();
          }
      
          public void Hide()
          {
              LogConsoleForm.Hide();
          }
      
          public void Focus()
          {
              LogConsoleForm.Focus();
          }
      
          public void WriteLine(string message)
          {
              LogConsoleForm.Write(message);
          }
      
          public void WriteLine(string message, bool error)
          {
              LogConsoleForm.Write(message, error);
          }
      
          public void WriteLine(string message, Exception ex)
          {
              LogConsoleForm.Write(message, (Exception)ex);
          }
      }
      
      public class LogConsoleWrapper : System.Windows.Forms.Form
      {
          /// /// Required designer variable.
      	/// 
      	/// 
      	private System.ComponentModel.Container components = null;
      
          static string sSource = "PIMSLite2008 Log Console";
          static string sLog = "Application";
      
          public LogConsoleWrapper()
      	{
      		InitializeComponent();
      
              if (!EventLog.SourceExists(sSource))
                  EventLog.CreateEventSource(sSource, sLog);
      
              clock.Start();
              lblUser.Text = System.Environment.UserName.ToString().ToUpper();
      	}
      
          /// /// Clean up any resources being used.
          /// 
          /// 
          protected override void Dispose(bool disposing)
          {
              if (disposing && (components != null))
              {
                  components.Dispose();
              }
              base.Dispose(disposing);
          }
      
          #region User Defined Code
      
          private void clock\_Tick(object sender, EventArgs e)
          {
              lblDate.Text = DateTime.Now.ToShortDateString();
              lblTime.Text = DateTime.Now.ToLongTimeString();
          }
      
          public void Write(string message)
          {
              string\[\] tmp = new string\[3\];
      
              tmp\[0\] =
      
      B Offline
      B Offline
      BlitzPackage
      wrote on last edited by
      #2

      I would try to declare it as a member of the Application class. Then, you can refer to it by accessing the application object. I believe you can get access to the application object anywhere in your code. I hope that helps.

      1 Reply Last reply
      0
      • T tkrn

        I am trying to create a Log Console that will be a hidden form that will called up when LogConsole.Show() is issued, and write to the Data Grid that is not bound by using the methods as defined. I never done anything like that and I need direction. Take a look at the code below to get a feel for what i am trying to do.

        namespace System.Diagnostics
        {
        public class LogConsole
        {
        public static readonly LogConsole Instance = new LogConsole();
        private LogConsoleWrapper LogConsoleForm = new LogConsoleWrapper();

            public void Show()
            {
                LogConsoleForm.Show();
            }
        
            public void Hide()
            {
                LogConsoleForm.Hide();
            }
        
            public void Focus()
            {
                LogConsoleForm.Focus();
            }
        
            public void WriteLine(string message)
            {
                LogConsoleForm.Write(message);
            }
        
            public void WriteLine(string message, bool error)
            {
                LogConsoleForm.Write(message, error);
            }
        
            public void WriteLine(string message, Exception ex)
            {
                LogConsoleForm.Write(message, (Exception)ex);
            }
        }
        
        public class LogConsoleWrapper : System.Windows.Forms.Form
        {
            /// /// Required designer variable.
        	/// 
        	/// 
        	private System.ComponentModel.Container components = null;
        
            static string sSource = "PIMSLite2008 Log Console";
            static string sLog = "Application";
        
            public LogConsoleWrapper()
        	{
        		InitializeComponent();
        
                if (!EventLog.SourceExists(sSource))
                    EventLog.CreateEventSource(sSource, sLog);
        
                clock.Start();
                lblUser.Text = System.Environment.UserName.ToString().ToUpper();
        	}
        
            /// /// Clean up any resources being used.
            /// 
            /// 
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
        
            #region User Defined Code
        
            private void clock\_Tick(object sender, EventArgs e)
            {
                lblDate.Text = DateTime.Now.ToShortDateString();
                lblTime.Text = DateTime.Now.ToLongTimeString();
            }
        
            public void Write(string message)
            {
                string\[\] tmp = new string\[3\];
        
                tmp\[0\] =
        
        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        I'm unclear as to what the problem is. Public classes are available "throughout an entire project/application". Is there a more specific problem you're encountering? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        B 1 Reply Last reply
        0
        • M Mark Salsbery

          I'm unclear as to what the problem is. Public classes are available "throughout an entire project/application". Is there a more specific problem you're encountering? Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          B Offline
          B Offline
          BlitzPackage
          wrote on last edited by
          #4

          Mark brings up a good point. However, I think he needs a place to put an instantiated object of that class. I know in WPF Applications there is a App.xaml.cs file that gives you access to the application object. However, I am not sure where to find the App.cs file in Windows Forms applications.

          M 1 Reply Last reply
          0
          • B BlitzPackage

            Mark brings up a good point. However, I think he needs a place to put an instantiated object of that class. I know in WPF Applications there is a App.xaml.cs file that gives you access to the application object. However, I am not sure where to find the App.cs file in Windows Forms applications.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            I'm still unclear how the OP title relates to the code (mostly because my attention span won't let me read a code snippet that big :)), but the

            public static readonly LogConsole Instance = new LogConsole();

            makes "Instance" globally available since the LogConsole class is public. I'm not sure what's not working :) Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            B 1 Reply Last reply
            0
            • M Mark Salsbery

              I'm still unclear how the OP title relates to the code (mostly because my attention span won't let me read a code snippet that big :)), but the

              public static readonly LogConsole Instance = new LogConsole();

              makes "Instance" globally available since the LogConsole class is public. I'm not sure what's not working :) Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              B Offline
              B Offline
              BlitzPackage
              wrote on last edited by
              #6

              Good point. Where would he declare it?

              M 1 Reply Last reply
              0
              • B BlitzPackage

                Good point. Where would he declare it?

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                I copied that from the 5th line of his code :)

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups