views:

112

answers:

7

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?

+3  A: 

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 */
Snake
A silly oversight! Thanks for pointing that out.
Jimbo
No problem, please accept the answer :)
Snake
I have updated the code above, it still wont compile and I dont understand why (with no definitive error from the compiler either!)
Jimbo
Ok, found the problem, the class declaration must define the generic object TModel, not the constructor. Thanks for your input.
Jimbo
A: 

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.

Darin Dimitrov
A: 

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

tanathos
A: 

The constructor has to be

public ScrollableCheckboxList<TModel>

rather than

public void ScrollableCheckboxList<TModel>

In other words, drop the void.

Daniel Rose
+4  A: 

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(...) 
  {
    // ...
  }
}
The Josenator
+2  A: 

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)
    { 
        ...
Jimbo
+2  A: 

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.

Dave