イーサネットコマンドを使用してテスト機器とインターフェイスするためのクラスライブラリを作成しています。デバイスには複数の軸を定義できます。メインクラスはコマンドを送受信できる必要があります。また、各軸はコマンドを送受信できる必要があります。ここに例があります:ネストされた内部クラス:ネストされたクラスタイプの変数を公開して親メソッドにアクセスする
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
}
}
}
}
私は2つの異なる問題があります。第1の理由は、Axes
の下に線public Axis[] Axes
の赤い波打ちがあり、AxisがAxesよりアクセスしにくいためです。 2番目の問題は、TestEquipment
クラスとその内部Axis
クラスのSendReceive
の使用方法がわかりません。
Axesの問題は、入れ子になっていなくても公開されていても解決できますが、の代わりにAxis
を作成したくありません。
メソッドとTcpClientとNetworkStreamを静的クラスに入れて静的クラスにして静的にしても醜いように見える場合は、両方のクラスでSendReceive
を使用できます。ここで
は、それがどのように使われるかの簡単な抜粋です:
var device = new TestEquipment();
device.Initialize();
device.Axes[0].Angle = 90;
これらはTestEquipment
クラスの外で可能にすべきではない:
device.SendReceive("");
var newAxis = new Axis();
は、残念ながら、私は私の実際のコードを共有することはできませんよ必要に応じて、私のサンプルコードに追加することができます。それ以上の明確化が必要な場合、私はそうすることを喜んでします。
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("");
}
}
}
}
あなたは、単純な 'Axis'ためのパブリックインターフェイスを持っている可能性があり、アクセシビリティの問題については、 'IAxis'と言って、' Axes'を 'public IAxis [] Axes;'と定義します。 –
もう一つの問題としては、 'Axis'が' TestEquipment'のインスタンスをコンストラクタに受け取り、それをプライベートプロパティに設定して、それを構築するときに 'new Axis(this) 'を実行することができます。このようにして、そのインスタンスから 'send' /' receive'メソッドを使うことができます。 –
ありがとう@kopranb。私の問題は完全に解決されました。 – ozziwald