Zapnologica
Zapnologica

Reputation: 22556

C# - access a global variable in my main form from a user control

I have my main Form called Form1 with code Program.cs In program.cs I have a global reference to a com port library which I have written. But now in my form I have user controls. These user controls need to be able to access the Com port library.

Here is my code in my main program:

 namespace robot_client
{
   static class Program
  {
    public static SerialReaderWriter serialReaderWriter = new SerialReaderWriter();
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {       
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
     }
   }
}

Now I need to be able to Access serialReaderWriter in my User Control Class.

Upvotes: 0

Views: 1059

Answers (1)

Cyril Gandon
Cyril Gandon

Reputation: 17058

From your Form1, you can get your static property by calling it via the Program class:

public class Form1()
{
    SerialReaderWriter myComObject = Program.serialReaderWriter;
}

Your user control can access it the same way.

Upvotes: 3

Related Questions