tags:

views:

111

answers:

4
+5  Q: 

.NET Results Class

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?

+2  A: 

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.

Amry
A: 

I don't think there is anything standard that will do this for you.

A great example of using out parameters, is the TryParse functions.

Daniel A. White
I think the TryParse is in the way it is simply to be backward compatible. Assuming if the Nullable was already there when they created TryParse, they would prefer (at least I will) to return Nullable<T> for TryParse instead of using the out parameter for the value and the Boolean return to specify whether it successfully parsed the input string or not.
Amry
+5  A: 

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).

Tomas Petricek
"not worth declaring a new class to hold the results"? Really?
pdr
I wouldn't use `Tuple<..>` in public members of the class. In a private member that can be used only from a limited number of places it may be easier to use it rather than construct a single-purpose class.
Tomas Petricek
Hmm, ok, each to their own. For 6 lines of code, I'd create a private child class just for clarity, but that's not to claim superiority, I just get this nasty squirmy feeling when I use a class for something other than the intended purpose.
pdr
I see this is listed as one of the common usages of Tuple here (http://msdn.microsoft.com/en-us/library/dd268536.aspx) so fair enough. The name Tuple to me implies one of the first two listed purposes.
pdr
@pdr: As everything, this depends on personal preferences and on the type of project and the current phase of the project. For example, Tuples are perfect for prototyping.
Tomas Petricek
+1 for Tuple and automatically implemented properties. However, I would have recommended anonymous types, too.
Venemo
@Venemo: Anonymous types won't work well if you need to return something as a result of a (non-generic) method, as they can be used only locally.
Tomas Petricek
That is true. :)
Venemo
A: 

If you have two values to return, use one as the return value and one as an out parameter.

If you have any more, create your own return value class and return that.

James Curran
I can agree with this if the usage scenario, in a way, felt just right, like the TryParse method. But it will feel weird if the return value is an int representing the age of the person where the name of the person is returned via a string out parameter. It's like the values are contextually related, but disconnected in the way they are returned to the caller.
Amry