C # access the parent class methods of another file

advertisements

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.


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?