views:

145

answers:

4

I know there are very similar questions but im not sure that any of them are exactly what i need. I have 2 methods that do exactly the same thing (so i dont need to override or anything) the only difference is the parameter and return types.

public List<List<TestResult>> BatchResultsList(List<TestResult> objectList)
 {

 }

public List<List<ResultLinks>> BatchResultsList(List<ResultLinks> objectList)
 {

 }

is there a neat way of doing this that doesnt involve duplciate code (the types are used inside the method).

+1  A: 

what about

public List<IList> BatchResultsList(List<IList> objectList)
{
}
twk
How would i call it using IList?
xoxo
I would recommend the solution presented by Rytmis - generics provide you with better type control.
twk
A: 

Generic version:

public List<List<T>> BatchResultsList<T>(List<T> objectList){}
Vitaliy Liptchinsky
+2  A: 

use generic methods

public List<List<T>> BatchResultsList<T>(List<T> objectList)
{

}

when you call it for TestResult:

BatchResultsList<TestResult>(testResultList)

for ResultLinks:

BatchResultsList<ResultLinks>(resultLinksList)

EDIT:

I presume that because it's the same code inside you 2 methods, TestResult & ResultLinks must implement a common interface, let's call it SomeInterface & a common constructor, let's choose the parameterless one:

you would declare and use the method like this:

    public List<List<T>> BatchResultsList<T>(List<T> objectList) 
     where T:SomeInterface, new()
    {
        List<List<T>> toReturn = new List<List<T>>();

        //to instantiate a new T:
        T t = new T();

        foreach (T result in objectList)
        {
            //use result like a SomeInterface instance
        }
        //...
        return toReturn;
    }
najmeddine
And maybe add a where T: ... to restrict the T to some baseclass or interface.
Hans Kesting
Ok but i cant type 'T' in my project? it says the namespace cannot be found? also how would i use that type inside the method? e.g. foreach(T result in objectlist) { }
xoxo
+6  A: 
public List<List<T>> BatchResultsList<T>(List<T> objectList)
{
    foreach(T t in objectList)
    {
        // do something with T.
        // note that since the type of T isn't constrained, the compiler can't 
        // tell what properties and methods it has, so you can't do much with it
        // except add it to a collection or compare it to another object.
    }
}

and if you need to limit the type of T so that you'll only process specific sorts of objects, make both TestResult and ResultLinks implement an interface, say, IResult. Then:

public interface IResult 
{
    void DoSomething();
}

public class TestResult : IResult { ... }

public class ResultLinks : IResult { ... }

public List<List<T>> BatchResultsList<T>(List<T> objectList) where T : IResult
{
    foreach(T t in objectList)
    {
        t.DoSomething();
        // do something with T.
        // note that since the type of T is constrained to types that implement 
        // IResult, you can access all properties and methods defined in IResult 
        // on the object t here
    }
}

When you call the method, you can of course omit the type parameter, since it can be inferred:

List<TestResult> objectList = new List<TestResult>();
List<List<TestResult>> list = BatchResultsList(objectList);
Rytmis
i cant type 'T' in my project? it says the namespace cannot be found? also how would i use that type inside the method? e.g. foreach(T result in objectlist)
xoxo
What do you mean you can't type 'T' in your project? Did you notice that I corrected my answer and amended the method signature to read BatchResultsList<T>? I forgot that when I first posted it. And yes, foreach(T result in objectList) is exactly the syntax for iterating the items. However, if you want to actually *do* something with them, you need to use a constraint to tell the compiler f.ex. that "every T must be IResult", so you can use T as if it was an IResult.
Rytmis
yeah its the <T> after the method call that was missing. thanks.
xoxo