C # constructor issue when using generics

See an example of my code below:

UPDATED CODE

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 made for the constructor declaration! Another problem with this code though

The code will not compile - it encounters a lot of strange little bugs that make me think that this is a design problem?

+3
source share
7 answers

As others pointed out, you should opt out of the keyword void, however it is still incorrect. The generic declaration must be in the class, not in the constructor

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

The name of the function ScrollableCheckboxListmatches the name of your class.

, - .

, , void , # . , , (, ).

, void , .

TModel .

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

TModel,

.

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

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

, . , , .

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()
            });
        }
    }
}

, - List .

+2

. void:

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

, .

0

This is not a constructor. To be a constructor, you must remove the "void".

0
source

The constructor should be

public ScrollableCheckboxList<TModel>

but not

public void ScrollableCheckboxList<TModel>

In other words, remove the void.

0
source

Source: https://habr.com/ru/post/1747149/


All Articles