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?
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 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 */
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)
{
...
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.
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.
The constructor has to be
public ScrollableCheckboxList<TModel>
rather than
public void ScrollableCheckboxList<TModel>
In other words, drop the void.
It's not a constructor, to be a constructor you've to remove the "void" keywork.
精彩评论