0

I'm reading from a database and trying to store the current record in an instance of my object. How can I add a new item to my list?

private void Load_data<T>(AseDataReader reader, List<T> table)
    {
        while (reader.Read())
        {
            table.Add(new myclass1
            {
                id_number= SafeGetInt(reader, 0),
                state = SafeGetString(reader, 1)
            });
        }
    }

The "table.Add(new myclass1" is the issue for the compiler. More specifically the, "myclass1". I'm new to generics. How can I do this? Is it possible?

Update: Okay so I'm calling the above method when I get to the point in my code that I know what type it needs to be. I'm using a method one level up from this that accepts all list types - so I don't need to have 12 overloaded methods that do the same thing.

Anton
  • 761
  • 17
  • 40
  • 5
    If you know you are going to insert a specific type into the list, then why would you need it to be generic? – juharr Mar 20 '15 at 17:33
  • You can't to something like that as you don't know what `T` will be at runtime. Could be int, which doesn't hold an `id_number` property. – xlecoustillier Mar 20 '15 at 17:33
  • 1
    Have a look at this questions: http://stackoverflow.com/questions/731452/create-instance-of-generic-type Basically you need to create instances of the generic type. If your constructor doens't accept the parameters you may need to use reflection to access the properties. By the way, if your list is generic how do you know objects are going to have id_number and state? – Juan Mar 20 '15 at 17:34
  • @Juan, is there a way to assign values to the properties without knowing their names? – Anton Mar 20 '15 at 17:38
  • generics doesnt mean you can define any class on the fly. you still have to define myclass1 somewhere. It could work fine it was a primitive type instead. have you defined class myclass1? – starthis Mar 20 '15 at 17:39
  • @Sk1, yes, myclass1 is defined along with 11 other classes. I'm trying to make a generic enough method so that I can pass in whichever empty list I need and then add items to it. I'll check for which class it is and call the appropriate method. In this case I called the method above and now want to add items to it. – Anton Mar 20 '15 at 17:53
  • @SK1, meant to add, at this point in my code I know what type of list it is and called the appropriate method (above). – Anton Mar 20 '15 at 18:00

1 Answers1

4

You either don't need to use generics

private void Load_data(AseDataReader reader, List<myclass1> table)
{
    while (reader.Read())
    {
        table.Add(new myclass1
        {
            id_number= SafeGetInt(reader, 0),
            state = SafeGetString(reader, 1)
        });
    }
}

Or you need a way to tell your method how to create something of type T

private void Load_data<T>(
    AseDataReader reader, 
    List<T> table, 
    Func<AseDataReader, T> create)
{
    while (reader.Read())
    {
        table.Add(create(reader));
    }
}

And call it like this

List<myclass1> table = new List<myclass1>();
Load_data(
    reader, 
    table, 
    r => new myclass1
    {
       id_number= SafeGetInt(reader, 0),
       state = SafeGetString(reader, 1)
    });
juharr
  • 31,741
  • 4
  • 58
  • 93