1

I have a winforms applications which should be able to communicate with a custom device. I have a main window which has a button "Disconnect". I have a user control which handles the logging, and a class (SerialCom) which handles all communciation.

When I load the user control I also setup the com port in the SerialCom class. This class receives data, and sends it to the user control via INotifyPropertyChanged, see this question for more detail here.

I want to acces the class SerialCom from the MainForm, without having to new the SerialCom class. but the SerialCom class is first created on the user control. (so, if I new the SerialCom class from the MainForm, the serial port will not be open, so I cannot close it.)

Any tips on how to acces class A from class B and C without having to new class A again? I was thinking about passing the event between the forms, is this the best option?

5
  • 2
    there are many options here: you could pass a reference to the object from A to B, you could have a "program" class which is accessible by any form in your application. You could have a delegate for an event. You could use an observer/observable construction. The world is at your feet. :) Commented Nov 20, 2012 at 8:44
  • question updated to make more clear Commented Nov 20, 2012 at 8:47
  • 1
    Would it help to make SerialCom a singleton? Commented Nov 20, 2012 at 8:53
  • just expose SerialCom instance from your UserControl using property and add the event handler in your form. Commented Nov 20, 2012 at 8:54
  • @ThorstenDittmar thanks this did work, I tried this before but could not figure how to get it working, and now it does work perfectly fine :) Thanks! Commented Nov 20, 2012 at 9:01

2 Answers 2

1

Turn your SerialCom class into a singleton. That way you have an instance that many classes can access. But please make sure to make this thread safe!

See this for making thread safe singletons.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I used the thread safe example from microsoft indeed. It works as expected :)
1

Perhaps you are looking for a singleton. Basically, a single is an object that only allows a single instance to be initialised, and provides a mechanism (through static methods/properties) to access this instance from anywhere.

Here is a simple implementation that i found at the following link http://msdn.microsoft.com/en-us/library/ff650316.aspx

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}

1 Comment

Which it even says in the article. =D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.