2016-10-06 18 views
1

メソッドでListまたはintを返すことはできますか?C#戻り値とエラー処理

ような何か:

  • それが成功した場合:戻りリスト
  • を、それが失敗した場合:リターンint型(つまり、エラー番号を示します)、または、エラーメッセージの文字列を。

(例外はありませんが、値は正しくありません。たとえば、PointF.X = 0または最低値が10以上です)。

は今、私はこのようにそれをやっている:私はこのようにそれを行うと

public List<PointF> GetContourInfoFromFile(string a_file) 
    { 
     ListContourPoints.Clear(); 
     List<string> textLines = new List<string>(); 

     bool inEntitiesPart = false; 
     bool inContourPart = false; 

     try 
     { 
      foreach (string str in File.ReadAllLines(a_file)) 
      { 
       textLines.Add(str);//Set complete file in array 
      } 

      for (int i = 0; i < textLines.Count; i++) 
      { 
       //read complete file and get information and set them in ListContourPoints 
      } 

      //Check Coordinate values 
      for (int i = 0; i < ListContourPoints.Count; i++) 
      { 
       //Coordinates are below -1! 
       if (ListContourPoints[i].X < -1 || ListContourPoints[i].Y < -1) 
       { 
        ListContourPoints.Clear(); 
        break; 
       } 
       //Lowest X coordinate is not below 10! 
       if (mostLowestXContour(ListContourPoints) > 10) 
       { 
        ListContourPoints.Clear(); 
        break; 
       } 
       //Lowest Y coordinate is not below 10! 
       if (mostLowestYContour(ListContourPoints) > 10) 
       { 
        ListContourPoints.Clear(); 
        break; 
       } 
      } 
     } 
     catch (Exception E) 
     { 
      string Error = E.Message; 
      ListContourPoints.Clear(); 
     } 
     return ListContourPoints; 
    } 

、私は、TEの値に異常があると知っている..しかし、具体的ではないものを。

どうすればこの問題を解決できますか?リストOR文字列/ intを返すことができない場合は、最適な解決策は何ですか?

+0

は、検証は、あなたが言及したエラー番号を与えることができます多分カスタム例外を失敗したときに例外を自分で投げますか? – Stefan

+1

はい、値が範囲外の場合は、実際に例外をスローする必要があります。また、これらの値がユーザによって入力された場合は、 'GetContourInfoFromFile()'に渡す前に入力をサニタイズするための別の検証メソッドが必要です(検証メソッドは例外をスローする必要はありません。成功時には 'null'、失敗時にはエラーメッセージ文字列を返すことができます。 –

+0

ファイル内の値が正しくない場合は、通常の動作を終了する必要があります。例外は良い解決策です。しかし、ファイルが正しくないことをユーザーに通知するために、例外がより高いレベルで捕捉されていることを確認する必要があります。 –

答えて

2

することはでき

解決方法1:

スロー例外場合erreur、上のコードでtry catchを実行してください

はあなたの機能で一部キャッチを削除し、あなたが機能を呼び出すときに、このようにトライキャッチでそれを行う:

try{ 
    List<PointF> result=GetContourInfoFromFile(youfile); 
    } 
    catch (Exception E) 
    { 
     string Error = E.Message; 
    } 

解決方法2:

リターン財産

としてListresultを持つオブジェクトとエラー
+0

値が間違っていると、通常の処理が「終了」する必要があるので、私は解決策1に行きます。 –

0

一つの解決策は、オブジェクト

public object GetContourInfoFromFile(string a_file) 
    { 

    } 

、あなたは、これはint型とリストの両方にキャストしようと成功した1参照呼び出す方法でを返すようになります。

より精巧な解決策は、クラスに

public class YourClassName{ 
    public List<PointF> YourList {get; set;} //for success 
    public int YourVariable {get; set} // for failure 
    public string YourMEssage {get; set} // for failure 
} 

を持って返すことであろうその1

public YourClassName GetContourInfoFromFile(string a_file) 
    { 

    } 
1

数値を返す代わりに、catchブロックから例外をスローして、外部例外ハンドラがキャッチできるようにすることができます。ここで

はサンプルです:

public void MainMethod() 
    { 
     try 
     { 
      var myList = SomeMethod(); 
     } 
     catch(Exception ex) 
     { 
      Console.WriteLine(ex.Message); // prints out "SomeMethod failed." 
     } 
    } 

    public List<object> SomeMethod() 
    { 
     try 
     { 
      int i = 1 + 1; 

      // Some process that may throw an exception 

      List<object> list = new List<object>(); 
      list.Add(1); 
      list.Add(i); 

      return list; 
     } 
     catch(Exception ex) 
     { 
      Exception newEx = new Exception("SomeMethod failed."); 
      throw newEx; 
     } 
    } 
0

あなたが値またはエラーコードのいずれかを保持しているラッパークラスを作成することができます。例:

public class Holder<T> 
{ 
    private Holder(T value) 
    { 
     WasSuccessful = true; 
     Value = value; 
    } 

    private Holder(int errorCode) 
    { 
     WasSuccessful = false; 
     ErrorCode = errorCode; 
    } 

    public bool WasSuccessful { get; } 
    public T Value { get; } 
    public int ErrorCode { get; } 

    public static Holder<T> Success(T value) 
    { 
     return new Holder<T>(value); 
    } 

    public static Holder<T> Fail(int errorCode) 
    { 
     return new Holder<T>(errorCode); 
    } 
} 

使用法:あなたは考えがあり

public Holder<PointF> MyFunc() 
{ 
    try 
    { 
     // 
     return Holder<PointF>.Success(new PointF()); 
    } 
    catch 
    { 
     return Holder<PointF>.Fail(101); 
    } 
}