2016-07-30 4 views
1

複数の情報を返すために参照パラメータを使用しています。以下のように、C#参照パラメータの使用

int totalTransaction = 0; 
int outTransaction = 0; 
int totalRecord = 0; 

var record = reports.GetTransactionReport(searchModel, out totalTransaction, out outTransaction, out totalRecord); 

//および方法は、このようなものです

public List<TransactionReportModel> GetAllTransaction(
      TransactionSearchModel searchModel, 
      out totalTransaction, 
      out totalTransaction, 
      out totalRecord) { 


    IQueryable<TransactionReportModel> result; 
    // search 

    return result.ToList(); 
} 

しかし、私は長いパラメータを好きではないので、私は辞書を使用して、単一のパラメータとのことをクリーンアップしようとしています。

Dictionary<string, int> totalInfos = new Dictionary<string, int> 
{ 
    { "totalTransaction", 0 }, 
    { "outTransaction", 0 }, 
    { "totalRecord", 0 } 
}; 

var record = reports.GetTransactionReport(searchModel, out totalInfos); 

しかし、キーストリングが約束されていないため、まだ十分ではないので、それは難しいコーディングのようです。

キーにConstantを使用する必要がありますか?そのような場合にはより良い解決法がありますか?

+3

プロパティを使用してすべての情報を公開するクラスを作成するだけではどうですか。 –

+1

これらの警告のすべてが重要ではありませんが、私はこれに同意します:https://msdn.microsoft.com/en-us/library/ms182131.aspx「out」パラメータが必要な理由を本当に理解していない限り、私は避けるでしょうそれら。 – starlight54

答えて

5

クラスを使用するだけです。 outのパラメータは完全に避けてください。

class TransactionResult 
{ 
    public List<TransactionReportModel> Items { get; set; } 

    public int TotalTransaction { get; set; } 
    public int OutTransaction { get; set; } 
    public int TotalRecord { get; set; } 
} 


public TransactionResult GetAllTransaction(TransactionSearchModel searchModel) 
{ 
    IQueryable<TransactionReportModel> result; 
    // search 

    return new TransactionResult 
    { 
     Items = result.ToList(), 
     TotalTransaction = ..., 
     OutTransaction = ..., 
     TotalRecord = ... 
    }; 
} 
+0

ありがとう! –

関連する問題