Can I use a class for template parameters in C #?

Source: Internet
Author: User

Recently, when I was making a software, because of the use of multiple document forms, the menu in Mdicontrainer is mainly used to display the subform, the number of these menus, the menu programming I want to use generic programming, the source code is as follows (a menu)://T as a template parameter, here want to pass a class name, but ....
private void Showmdichild < T > (string mdichildname) where T:icomparable < T >
... {
T FM;
BOOL Hasquery=false;
foreach (Form frm in this. Mdichildren)
if (frm. Name = = mdichildname)
... {
frm. Activate ();
Hasquery = true;
}
if (! Hasquery)
... {
FM = new T (); Can't do this new, hint: The variable type "T" has no new () constraint and cannot create an instance of the type
Fm. MdiParent = this; Nor, hint: "T" does not contain the definition of "mdiparent"
Fm.  Show (); Not yet, ditto.
}
}
MenuItem is a menu, Frmaddproblem is a form class and is, of course, inherited by the form class.
The Click event for this menu is used to open an Frmaddproblem form, and if an instance is already open, the
Will be turned on before activation, if not then new one
private void Menuitem_click (object sender, EventArgs e)
... {
Showmdichild<frmaddproblem> ("Frmaddproblem");
}

Error, see the code comments, do not know how to implement in the template function can use template class parameters to new instances of a class. Hurry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.