이 주제에 대해 이미 답변 된 유사한 질문을 모두 보았습니다. 그러나 정확한 문제를 해결하는 대답을 찾을 수 없었습니다.비 정적 필드에 객체 참조가 필요합니다 ...
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 행과 반대되는 개체 참조 오류가 발생했습니다.
그렇다면 나는 Return2code 메서드 대신 ReturnCode 반환 코드 섹션을 사용하여 완전히 분리 된 메서드를 만들려고 시도했지만 새로운 메서드를 정적으로 만들었지 만 여전히 오류가있었습니다.
는 또한
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);
}
편집 시도 : 그것은 ".. 개체 참조"를 수정 오류를 수행하지만 지금은 서비스 1이 포함되지 않은 말을 다른 오류를 제공하는 클래스 (내 나쁜)를 사용하고 있음 DaServerMgt..etc에 대한 정의. 누구든지 내가 뭘 잘못하고 있는지 알아?
메소드의 인스턴스를 초기화 할 수 없으므로 _ ** 소유 메소드 ** 클래스에 대해 수행해야합니다. –
코드는 어디에 있습니까? 'DaServerMgt.WriteAsync' –
프로 팁. '정적'사용을 피하십시오. 어렵고 빠른 규칙은 아니지만 그걸 가지고 실제로 나쁜 습관을 가질 수 있습니다. – BradleyDotNET