I am writing a class library to interface with a piece of test equipment through Ethernet commands. The device can have multiple axes defined. The main class will need to be able to send and receive commands. Also, each axis will need to be able to send and receive commands. Here is an example:
public class TestEquipment
{
private TcpClient _tcpClient;
private NetworkStream _networkStream;
public Axis[] Axes; // Variable should be accessible publicly
public TestEquipment()
{
Axes = new Axis[2];
Axes[0] = new Axis();
Axes[1] = new Axis();
}
public void Initialize()
{
// Use send/receive method to initialize the device
}
internal string SendReceive(string command)
{
return // Uses _tcpClient and _networkStream to talk to the device
}
internal class Axis
{
public double Angle
{
get
{
// Use send/receive method from parent class to get the axis angle
}
set
{
// Use send/receive method from parent class to set the axis angle
}
}
}
}
Now I have two different problems. The first is that I have red squiggles under Axes
for line public Axis[] Axes
because Axis is less accessible than Axes. The second problem is that I am not sure how to use SendReceive
from the TestEquipment
class and its inner Axis
class.
The Axes problem can be fixed if it is not nested and public, but I don't want any Axis
to be created outside of TestEquipment
.
I can use SendReceive
in both classes if I put the method and the TcpClient and NetworkStream in a static class and make them static but that seems ugly.
Here is a quick snippet of how it is used:
var device = new TestEquipment();
device.Initialize();
device.Axes[0].Angle = 90;
These should NOT be possible outside of the TestEquipment
class:
device.SendReceive("");
var newAxis = new Axis();
Unfortunately I am not able to share my actual code, so if needed, I can add on to my example code. If there is any more clarification needed I am happy to do so.
Answer
Here is the working code:
public class TestEquipment
{
private TcpClient _tcpClient;
private NetworkStream _networkStream;
public Axis[] Axes;
public TestEquipment()
{
Axes = new Axis[2];
Axes[0] = new Axis(this);
Axes[1] = new Axis(this);
}
public void Initialize()
{
// Use send/receive method to initialize the device
}
private string SendReceive(string command)
{
}
public class Axis
{
private TestEquipment _parent;
internal Axis(TestEquipment parent)
{
_parent = parent;
}
public double Angle
{
get
{
return _parent.SendReceive("");
}
set
{
value = _parent.SendReceive("");
}
}
}
}