Solved

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

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

13 Experts available now in Live!

Get 1:1 Help Now