Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Importing CSV file encoded in UTF-8 in SQL SERVER 2008 using BULK INSERT 5 50
Video Player 11 23
C# guarantee sql connection close 6 39
Keyboard 2 33
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 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