2011-10-07 3 views
0

I는 다음의 세 가지 열거가 1 : I의 시리얼 포트를 통해 센서 회로와 통신하고C# 메소드는 반환 가능한 세 열거 형

public enum SensorTypeA{A1,A2,...,A12}; 
public enum SensorTypeB{B1,B2,...,B12}; 
public enum SensorTypeC{C1,C2,...,C12}; 

와 'X'의 위치에서 사용되는 센서 보려는 그래서 메서드를 만들었습니다

public ???? GetSensorTypeAtLocation(int x) 
{ 
    ... 
    // Send serial command and receive response. 
    string responseCommand = SendReceive(String.Format("SR,ML,{0},\r", x)); 

    // Process response command string and return result. 
    return ???? (could be any of the 3 possible enums) 
} 

가능한 열거 형을 반환 할 수있는 방법이 있습니까? 출연 : object? 더 좋은 방법?

감사합니다.

EDIT

각 센서 유형의 다수의 센서가있다. 열거 형을 변경하여 이것을 반영합니다.

+1

그리고 어떤 경우 열거 값 중복? –

+1

세 개의 enum이있는 이유는 무엇입니까? 서로 다른 유형의 센서입니까? – ChrisF

+0

열거 형으로 할 수 있는지 모르겠다. 동일한 기본 클래스에서 파생 된 3 개의 클래스를 만들고 ClassI, ClassB 및 ClassC에 공개 열거 형 SensorType이 포함되도록합니다. –

답변

2

이것은 Enum.TryParse()에 대한 작업처럼 보입니다.

public Enum GetSensorTypeAtLocation(int x) 
{ 
    ... 
    // Send serial command and receive response. 
    string responseCommand = SendReceive(String.Format("SR,ML,{0},\r", x)); 

    //Try to parse the response into a value from one of the enums; 
    //the first one that succeeds is our sensor type. 
    SensorTypeA typeAresult; 
    if(Enum.TryParse(responseCommand, typeAResult)) return typeAresult; 
    SensorTypeB typeBresult; 
    if(Enum.TryParse(responseCommand, typeBResult)) return typeBresult; 
    SensorTypeC typeCresult; 
    if(Enum.TryParse(responseCommand, typeCResult)) return typeCresult; 
} 

이 문제는 당신이 반환 유형에 따라 과부하를 만들 수 있다는 것, 따라서 당신은 시스템이 돌아갑니다 정확히 알 수 없습니다 (그러나 CLR 런타임에 알, 당신은 유형을 심문 할 수 있습니다 특정 답을 얻기위한 반환 값).

A, B 및 C 값을 포함하는 Enum SensorType을 심각하게 고려할 것입니다.

public SensorType GetSensorTypeAtLocation(int x) 
{ 
    ... 
    // Send serial command and receive response. 
    string responseCommand = SendReceive(String.Format("SR,ML,{0},\r", x)); 

    // Process response command string and return result. 
    SensorTypeA typeAresult; 
    if(Enum.TryParse(responseCommand, typeAResult)) return SensorType.A; 
    SensorTypeB typeBresult; 
    if(Enum.TryParse(responseCommand, typeBResult)) return SensorType.B; 
    SensorTypeC typeCresult; 
    if(Enum.TryParse(responseCommand, typeCResult)) return SensorType.C; 
} 

지금, 당신은 반환 값 자체를 알고, 일 등의 일반 센서의 종류 : 그런 다음 기능은 센서가 준 응답의 유형에 따라 명확한 답을 반환 할 수 있습니다.

+0

나는 당신의 첫 제안을 할 것이라고 생각합니다. 난 게시물을 조금 단순화했다. 열거 형 중 두 가지는 센서 유형이고, 하나는 "계산 유형"이다. 이 계산 유형은 ADD, SUB ...입니다. 여기서 ADD는 SensorTypeA.A12 + SensorTypeB.2 또는 일부 다른 조합과 같을 수 있습니다. 이 때문에, 나는 런타임에 심문해야하지만 너무 고통스럽지 않아야합니다. 감사! – john

0

질문의 루트가 "함수가 여러 유형을 반환 할 수 있습니까?"라는 질문을 완전히 무시하면 무시할 수 있습니다. Enum 이에 대한 대답은 "예"이지만이 경우 Object에, 높은 수준의 클래스를 반환해야하고 반환 후 유형 검사를 수행해야합니다 :

public enum SensorTypeA { A = 0 }; 
    public enum SensorTypeB { B = 1 }; 
    public enum SensorTypeC { C = 2 }; 

    private object GetSensorTypeAtLocation() 
    { 
     return SensorTypeB.B; 
    } 

    private void YourMethod(object sender, EventArgs e) 
    { 
     object value = GetSensorTypeAtLocation(); 
     if (value is SensorTypeA) 
     { 
      Console.WriteLine("A"); 
     } 
     else if (value is SensorTypeB) 
     { 
      Console.WriteLine("B"); 
     } 
     else if (value is SensorTypeC) 
     { 
      Console.WriteLine("C"); 
     } 
     else 
     { 
      Console.WriteLine("Unknown"); 
     } 
    }