Expert wanna be
Expert wanna be

Reputation: 10624

C# reference parameter usage

I'm using reference parameter for returning multiple information. like,

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

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

// and the method is like this,

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


    IQueryable<TransactionReportModel> result;
    // search

    return result.ToList();
}

But I don't like the long parameters, so I'm trying to cleaning that up with single parameter, using Dictionary.

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

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

But still not good enough, because the key strings are not promised, it's like hard cording.

Do I need to use Constant for the keys? or any better solution for that case?

Upvotes: 1

Views: 73

Answers (1)

Eli Arbel
Eli Arbel

Reputation: 22739

Just use a class. And avoid out parameters completely:

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 = ...
    };
}

Upvotes: 5

Related Questions