Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Results Class

Tags:

c#

.net

I have a C# function that does some processing and needs to return figures about what it has processed, success, failure, etc.

Can anyone tell me if there are any standard framework classes that will handle this (for example, number of records processed, number successful, failed, etc) ?

Alternatively, can anyone say how they usually handle this? Is the best way to create a class, or to use out parameters?

like image 603
Paul Michaels Avatar asked May 01 '10 13:05

Paul Michaels


2 Answers

I don't think there is any standard class for representing this kind of information. The best way to handle this is probably to define your own class. In C# 3.0, you can use automatically implemented properties, which reduce the amount of code you need to write:

class Results {
  public double Sum { get; set; }
  public double Count { get; set; }
}

I think out parameters should be used only in relatively limited scenarios such as when defining methods similar to Int32.TryParse and similar.

Alternatively, if you need to use this only locally (e.g. call the function from only one place, so it is not worth declaring a new class to hold the results), you could use the Tuple<..> class in .NET 4.0. It allows you to group values of several different types. For example Tuple<double, double, double> would have properties Item1 ... Item3 of types double that you can use to store individual results (this will make the code less readable, so that's why it is useable only locally).

like image 136
Tomas Petricek Avatar answered Sep 28 '22 00:09

Tomas Petricek


I don't think there is any built in classes for that. Usually I will create my own class to accommodate the kind of result you were talking about, and yes, I prefer a Result class instead of out parameters simply because I feel it's cleaner and I'm not forced to prepare variables and type in the out parameters every time I need to call that function.

like image 31
Amry Avatar answered Sep 27 '22 23:09

Amry