How do I have a class globally accessible throughout an entire project/application
-
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\] =
-
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\] =
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.
-
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\] =
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:
-
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:
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.
-
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.
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:
-
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:
Good point. Where would he declare it?
-
Good point. Where would he declare it?
I copied that from the 5th line of his code :)
Mark Salsbery Microsoft MVP - Visual C++ :java: