Solved

Syntax for method to shape data table into collection of objects (can't get generics right)

Posted on 2013-11-19
2
252 Views
Last Modified: 2013-11-19
Here's what I'm trying to accomplish:

public TCollection<TInterface> ShapeDataTable<TInterface, TImplementation, TCollection>(DataTable dataTable)
      where TImplementation : TInterface
      where TCollection : List
{
    TCollection<TInterface> _returnData = new TCollection<TInterface>();
    if (dataTable.Rows.Count > 0)
    {
        foreach (DataRow row in dataTable.Rows)
        {
            TInterface dataobj = Activator.CreateInstance<TImplementation>();
            ShapeRow<TInterface>(row, dataobj);
            _returnData.Add((TInterface)dataobj);
        }
    }
    return _returnData;
}

I'm getting errors on TCollection:
    "Using the generic type 'System.Collections.Generic.List<T> requires 1 type arguments"
    "The type parameter 'TCollection' cannot be used with type arguments

What do I need to do to get this method compiling?
0
Comment
Question by:FrancineTaylor
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39660320
Try these modifications:

public TCollection ShapeDataTable<TInterface, TImplementation, TCollection>(DataTable dataTable)
    where TImplementation : TInterface
    where TCollection : IList<TInterface>, new()
{
    TCollection _returnData = new TCollection();

    if (dataTable.Rows.Count > 0)
    {
        foreach (DataRow row in dataTable.Rows)
        {
            TInterface dataobj = Activator.CreateInstance<TImplementation>();
            ShapeRow<TInterface>(row, dataobj);
            _returnData.Add((TInterface)dataobj);
        }
    }
    return _returnData;
}

Open in new window

0
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 39660339
Perfect!  Thanks for your help, kaufmed.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question