Greatly in need some generics/type inference help

tempus

Member
Joined
Oct 8, 2013
Messages
11
Programming Experience
10+
Below is a simple code snippet based on existing code. For the life of me I just cant figure out how to pass that 'item' into the generic method. The error is on line 12 and is telling me the type argument cannot be inferred from the usage. I will admit, I did convert this code from some VB.NET code I have been using and the inference in VB.NET can be much looser so I'm sure that's where this all started. Any suggestions would be greatly appreciated.


class Program
    {
        static void Main(string[] args)
        {
            var foo = new ArrayList();
 
            var b = new bar() { foobar = "Test" };
            foo.Add(b);
 
            foreach (var item in foo)
            {
                tester(item);
            }
        }
 
        static void tester<T>(List<T> items)
        {
            //do something;
        }
    }
 
    public class bar
    {
        public string foobar { get; set; }
    }
 
 
You should use a List<bar> and pass that object to 'tester' since it expects a List<T> argument.
 
Excellent response however I realize I wasn't totally clear with the question/snippet. In the real case, the array list itself is a input parameter that then gets enumerated and its elements passed on (in this case tester()) so there is no way to know what object types are in the arraylist (in fact, the input parameter could be filled with arraylists of object).

Its really more like this


class Program
{
    static void Main(string[] args)
    {
        var foo = new ArrayList();
        var b = new bar() { foobar = "Test" };
        foo.Add(b);


        DoSomeWork(foo);
        
        var foo2 = new ArrayList();
        var b = new bar2() { fizz = "Test",buzz = "Test2" };
        foo2.Add(b);
        
        DoSomeWork(foo2);
        
    }


    static void DoSomeWork (ArrayList foo)
    {
        foreach (var item in foo) 
        {
            tester(item);
        }
    }


    static void tester<T>(List<T> items)
    {
        //do something;
    }
}


public class bar
{
    public string foobar { get; set; }
}


public class bar2
{
    public string fizz { get; set; }
    public string buzz { get; set; }
}




The code was written by a contractor about two years ago to create a generic Excel exporter and we are trying to bring in all these misc. utilities back into order. This is one of the out lying ones that we just can't seem to get our arms wrapped around.
 
If you're supposed input each item in 'foo' into 'tester' method then each item must be a List<T> or put in a List<T>, because that is the parameter type for that method.
ArrayList is really the .Net 1 equivalent of List<Object> before generics was introduced, so maybe you're just supposed to convert 'foo' to that.
 
That code really doesn't make much sense as it is. If you had code like that in VB then it must have been relying on late-binding, so probably bad code. You may have to use 'dynamic' in C#. Perhaps show us the VB code.
 

Latest posts

Back
Top Bottom