Solved

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

Posted on 2013-11-19
2
249 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now