C# Constructor Problem When Using Generics
        Posted  
        
            by Jimbo
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Jimbo
        
        
        
        Published on 2010-05-26T08:38:27Z
        Indexed on 
            2010/05/26
            8:41 UTC
        
        
        Read the original article
        Hit count: 227
        
Please see an example of my code below:
public class ScrollableCheckboxList
{
    public List<ScrollableCheckboxItem> listitems;
    public void 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()
            });
        }
    }
}
The code produces the following error:
'ScrollableCheckboxList': member names cannot be the same as their enclosing type
This clearly means that there is a method in the class that has the same name as the class, but usually insinuates that the method is trying to return something (which is not allowed)
In my case, all I have done is declare a constructor - why would this be a problem?
© Stack Overflow or respective owner