0

I have two forms: Form_Main and Form_Child and I have to instantiate Form_Child within Form_Main. Form main has a List and the constructor of Form_Child has a generic List. When I try to instantiate the child form, I get the following error message: Error 1 The non-generic type 'GenericParameterToFormConstructor.Form_Child' cannot be used with type arguments. The code of form_Main is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GenericParameterToFormConstructor
{
    public partial class Form_Main : Form
    {
        public Form_Main()
        {
            InitializeComponent();

            _list = new List<int>() { 1, 2, 3, 4, 5 };
            Form_Child child = new Form_Child<int>(_list);
        }

        private List<int> _list;
    }
}

The code of Form_Child is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GenericParameterToFormConstructor
{
    public partial class Form_Child<T> : Form
    {
        public Form_Child(List<T> list)
        {
            InitializeComponent();
        }
    }
}

What am I doing wrong? Please help. Thank you in advance.

user2102327
  • 59
  • 2
  • 6
  • 19
  • You need to specify the type argument before the variable name as well. – Ron Beyer Nov 19 '19 at 17:27
  • Don't defile the Form's type/constructor. You can use a public method in `Form_Child` that can accept `` (e.g., `public void MyPublicMethod(IList myList)`). If your list is just of type `int`, you don't need a generic type. – Jimi Nov 19 '19 at 17:30
  • @ Ron Beyer Thank you, but it is not clear to me from your comment. Do you mind posting a short code example? – user2102327 Nov 19 '19 at 17:37
  • Call that method after you have a created the new insteance of `Form_Child`: `var child = new Form_Child(); child.MyPublicMethod(new List(new[] { 1, 2, 3, 4, 5 })); child.Show();` – Jimi Nov 19 '19 at 17:38
  • change it to `var child = new Form_Child(_list);` – Matt.G Nov 19 '19 at 17:39
  • @Jimi Thank you too. I do not always have only integers. I need the generic list in the constructor's parameters. – user2102327 Nov 19 '19 at 17:40
  • @Matt That does not work. I get the same error message as before. – user2102327 Nov 19 '19 at 17:41
  • The type of `T` is not the main concern here. You have to modify `Designer.cs`, too (it's a `partial` class), to accept `` and you also have to specify the type of `T` when you instantiate the class object: `var child = new Form_Child(...)`. I suggest to use a method to pass the list. – Jimi Nov 19 '19 at 17:52
  • That looks like the code for `Form2`, not `Form_Child` – Moho Nov 19 '19 at 18:05

1 Answers1

-1

There might be a Form_Child.designer.cs file too, containing the other bit of this partial class. I am pretty sure that does not have the generic parameter <T> in it since it is off the track to have form-derived classes with generic parameters.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32