私はすでにこのトピックに回答されているすべての同様の質問を見てきましたが、私は私の正確な問題を解決し、答えを見つけることができませんでした。オブジェクト参照が非静的フィールドのために必要とされる...など
private void Subscribe2Data()
{
// Define parameters for Subscribe method:
//int itemIndex;
//initialize the client subscription handle
clientSubscriptionHandle = 1;
//Paramter to specify if the subscription will be added as active or not
bool active = true;
// The updateRate parameter is used to tell the server how fast we
// would like to see data updates.
int updateRate = 1000;
// The deadband parameter specifies the minimum deviation needed
// to be considered a change of value. 0 is disabled
Single deadBand = 0;
// The revisedUpdateRate parameter is the actual update rate that the
// server will be using.
int revisedUpdateRate;
//Initialize the item identifier values
itemIdentifiers[0] = new ItemIdentifier();
itemIdentifiers[0].ClientHandle = 0;
itemIdentifiers[0].DataType = Type.GetType("System.int16");
itemIdentifiers[0].ItemName = "Channel1.Device1.Data1";
itemIdentifiers[1] = new ItemIdentifier();
itemIdentifiers[1].ClientHandle = 1;
itemIdentifiers[1].DataType = Type.GetType("System.int16");
itemIdentifiers[1].ItemName = "Channel1.Device1.Data2";
itemValues[0] = new ItemValue();
itemValues[0].Value = temp;
ReturnCode returnCode;
try
{
returnCode = DaServerMgt.WriteAsync(clientSubscriptionHandle, ref itemIdentifiers, itemValues);
if (returnCode != ReturnCode.SUCCEEDED)
{
Console.WriteLine("Write request failed");
}
}
catch (Exception ex)
{
Console.WriteLine("WriteAsync exception. Reason: ", ex);
}
returnCode = DaServerMgt.WriteAsync..etcと表示されています。私は、エラー "オブジェクト参照は、非静的なフィールド、メソッドまたはプロパティの"その行に必要ですが表示されます。他の同様の質問に対する答えを見て、私は "private void Subscribe2Data()"を "private static void Subscrive2Data()"に変更して、自分のメソッドを静的にしようとしました。ちょうどDaServerMgt.WriteAsyncラインとは対照的に、その方法で私の変数のすべての単一の1を作っ
やってはオブジェクト参照エラーがあります。
だから、私はSubscribe2Data方法の代わりに、それにRETURNCODEリターンコードセクションと完全に別の方法を作ってみましたし、新しいメソッドがstatic作ったが、エラーがまだそこにいました。
私も
ReturnCode returnCode;
try
{
Service1 p = new Service1();
returnCode = p.DaServerMgt.WriteAsync(clientSubscriptionHandle, ref itemIdentifiers, itemValues);
if (returnCode != ReturnCode.SUCCEEDED)
{
Console.WriteLine("Write request failed");
}
}
catch (Exception ex)
{
Console.WriteLine("WriteAsync exception. Reason: ", ex);
}
EDIT試してみました:修正し、「オブジェクト参照を..」エラーが、今、私はサービス1が含まれていないと言って他のエラーを与えるクラス(私の悪い)を使用していることDaServerMgt..etcのための定義。 誰かが私が間違っていることを知っていますか?
メソッドのインスタンスを初期化することはできません。メソッドのインスタンスを初期化することはできません。 –
このコードはどこにありますか? 'DaServerMgt.WriteAsync' –
プロチップ。 'static'を使わないでください。難しくて速いルールではありませんが、実際には*悪い習慣に陥る可能性があります。 – BradleyDotNET