Jimbo
Jimbo

Reputation: 22964

C# Constructor Problem When Using Generics

Please see an example of my code below:

CODE UPDATED

public class ScrollableCheckboxList
{
    public List<ScrollableCheckboxItem> listitems;

    public ScrollableCheckboxList<TModel>(IEnumerable<TModel> items, string valueField, string textField, string titleField) where TModel : class
    {
        listitems = new List<ScrollableCheckboxItem>();
        foreach (TModel item in items)
        {
            Type t = typeof(TModel);
            PropertyInfo[] props = new [] { t.GetProperty(textField), t.GetProperty(valueField), t.GetProperty(titleField) };
            listitems.Add(new ScrollableCheckboxItem
            {
                text = props[0].GetValue(item, null).ToString(),
                value = props[1].GetValue(item, null).ToString(),
                title = props[2].GetValue(item, null).ToString()
            });
        }
    }
}

EDIT Corrections to constructor declaration made! Still a problem with this code though

The code wont compile - it comes up with lots of strange little errors making me think that there's a design problem here?

Upvotes: 3

Views: 447

Answers (7)

Anemoia
Anemoia

Reputation: 8116

The name of the function ScrollableCheckboxList is the same as your classname.

The error itself is correct, your code is not.

You want to declare a constructor, but by adding void before the name of the constructor the C# compiler thinks it's a function. And functions cannot have the same name as the class they live in (hence the error).

So remove void in front of the name of the function, then it will be a constructor.

And specify the TModel constraints at class level.

public /* void */ ScrollableCheckboxList /* <TModel> */(IEnumerable<TModel> items, string valueField, string textField, string titleField) /* where TModel : class */

Upvotes: 3

Dave D
Dave D

Reputation: 8972

You can't have a constructor that takes generic parameters. You need to either move the generic param up to the class level or make the setting of items a method that takes a generic param.

public class ScrollableCheckboxList<TModel>
    where TModel : class
{
    public List<ScrollableCheckboxItem> listitems;

    public ScrollableCheckboxList(IEnumerable<TModel> items, string valueField, string textField, string titleField)
    {
        listitems = new List<ScrollableCheckboxItem>();
        foreach (TModel item in items)
        {
            Type t = typeof(TModel);
            PropertyInfo[] props = new [] { t.GetProperty(textField), t.GetProperty(valueField), t.GetProperty(titleField) };
            listitems.Add(new ScrollableCheckboxItem
            {
                text = props[0].GetValue(item, null).ToString(),
                value = props[1].GetValue(item, null).ToString(),
                title = props[2].GetValue(item, null).ToString()
            });
        }
    }
}

That should work fine, although I'd also recommend you don't expose the List member variable directly.

Upvotes: 2

Jimbo
Jimbo

Reputation: 22964

PROBLEM FOUND

The constructor may not declare the generic TModel definition, the class declaration must do that job

e.g.

public class ScrollableCheckboxList<TModel> where TModel : class
{ 
    public List<ScrollableCheckboxItem> listitems; 

    public ScrollableCheckboxList(IEnumerable<TModel> items, string valueField, string textField, string titleField)
    { 
        ...

Upvotes: 2

tanathos
tanathos

Reputation: 5606

It's not a constructor, to be a constructor you've to remove the "void" keywork.

Upvotes: 0

josef.axa
josef.axa

Reputation: 1173

As others have pointed out you should drop the void keyword, however it is still not correct. The generic declaration should be on the class, not the constructor

public class ScrollableCheckboxList<TModel>
  where TModel : class
{
  public ScrollableCheckboxList(...) 
  {
    // ...
  }
}

Upvotes: 4

Daniel Rose
Daniel Rose

Reputation: 17648

The constructor has to be

public ScrollableCheckboxList<TModel>

rather than

public void ScrollableCheckboxList<TModel>

In other words, drop the void.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

You didn't declare a constructor. There's no void keyword in a constructor:

public ScrollableCheckboxList<TModel>(IEnumerable<TModel> items, string valueField, string textField, string titleField) where TModel : class {}

A type cannot contain methods that have the same name as the type.

Upvotes: 0

Related Questions