views:

81

answers:

3

I was wondering if there was a way for an instance of a class to access its parent class's method(s) while preserving the data during runtime. (Did that make any sense?)

I have a ConnectionManager that spawns several Connectors (which is in charge of COM port communications) if there is a multiple COM Port setup.

file1.cs

namespace connector
{
public class connectormanager : idisposable
{
  public connectormanager(comport1, comport2, retries, delay)
  {
    // create dictionary that maps COMPorts with devices
    // **foreach spawn connector (if com port range is given)**
  }

  public searchchannel()
  {
    // search existing com ports
  }
}
}


file2.cs

namespace connector
{
  public class connector : idisposable
{
  public void connector(port, retries, delay)
  {
  // there is a timer with a timeout event defined
  }

  // com port read/write operations

  void _timer_Elapsed()
  {
    // **i want to access searchchannel**
    // problem is if i create a new instance of connectionmanager
    // i lose all the dictionary stuff
  }
}
}

both files are under the same solution and project. what I want to do is access search channel from _timer_Elapsed in file2.cs without creating a new instance of the connector manager.

Any advice or help greatly appreciated.

+1  A: 

You could make searchchannel static.

Read about it here.

Mike
+3  A: 

Your ConnectorManager looks like a factory for me. You shouldn't call Connector constructor by itself, but rather add a CreateConnector function in the ConnectorManager class

In this factory, you will call the Connector constructor, and pass to it the ConnectorManager instance.

The connector will store this instance, and use it as required.

public class ConnectorManager
{
  public Connector CreateConnector()
  {
      return new Connector(comport1, comport2, retries, delay,this);
  }
  [...]
}

public class Connector
{
  private ConnectorManager connectorManager;

  internal Connector(int comport1, int comport2, int retries, int delay, ConnectorManager manager)
  {
     connectorManager = manager;
     [...]
   }
   [...]
}

Notice that the Connector constructor is internal, you shouldn't call it from outside your assembly. You should NEVER instantiate a Connector without using ConnectorManager.CreateConnector()

If a read well your ConnectorManager constructor, the singleton pattern won't suit your need, are several ConnectorManager could be instantiated for different comport configuration, is it true?

Maupertuis
Thanks sir, this worked like a charm!
John H.
+1  A: 

You can make ConnectionManager as a Singleton object and since your connector class requires this dependency of ConnectionManager , you can inject it via property injection.

here is the code

 public interface IConnectionManager
         {
             void searchchannel() ;
         }


            public class connectormanager : IDisposable , IConnectionManager
               {
                     public static readonly connectormanager ConnectionManager = new connectormanager();

                      private connectormanager() 
                      { 
                        // create dictionary that maps COMPorts with devices 
                        // **foreach spawn connector (if com port range is given)** 
                      } 

                      private void searchchannel() 
                      { 
                        // search existing com ports 
                      } 



                    void  IConnectionManager.searchchannel()
                    {
                        searchchannel();
                    }

                 } 



         public class connector : IDisposable 
         {
               IConnectionManager _manager = null;
               public void connector(port, retries, delay)
               {
               }

             private IConnectionManager myManager
             {
                 get
                 {
                     return _manager;
                 }

                 set
                 {
                    myManager = connectormanager.ConnectionManager;
                 }

             }

             void _timer_Elapsed() 
             {
                   myManager.searchchannel();
             }


         }
saurabh
Isn't a singleton a little much for this scenario?
highphilosopher