Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7045133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:29:34+00:00 2026-05-28T02:29:34+00:00

I have the following winforms classes: class EntityEditorForm<T>: System.Windows.Forms.Form where T: ICloneable<T> {} class

  • 0

I have the following winforms classes:

class EntityEditorForm<T>: System.Windows.Forms.Form 
                              where T: ICloneable<T> {}

class EntityCollectionEditorForm<T> : System.Windows.Forms.Form 
                                      where T: ICloneable<T> {}

The first form class is an editor for <T> that creates controls at run-time depending on the type of T.

The second is a manager for a collection of <T> and has Add, Edit and Delete functions. The collection is displayed in a listview control with fields populated through reflection using custom attributes.

The code for Add and Edit buttons looks like this:

private void buttonEdit_Click (object sender, System.EventArgs e)  
{  
   T entity = default(T);  
   entity = (T) this.listView.SelectedItems[0].Tag;  
   new EntityEditor<T>(entity).ShowDialog(this);  
}

private void buttonEdit_Click (object sender, System.EventArgs e)  
{  
   T entity = new T();   //This is the code which is causing issues 
   entity = (T) this.listView.SelectedItems[0].Tag;  
   new EntityEditor<T>(entity).ShowDialog(this);  
}

The default(T) works in the case of edit but I’m having trouble with the Add scenario. T entity = new T(); does not appear to be legal.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:29:34+00:00Added an answer on May 28, 2026 at 2:29 am

    If your type contains an parameterless constructor, you can add a constraint onto your generic type T to allow instantiation through this parameterless constructor. To do this, add the constraint:

    where T : new()
    

    MSDN Article on Constraints on Type Parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in VS2010 Ultimate: class MyComboBox : System.Windows.Forms.ComboBox { public
I have a winforms application that is doing the following: on each query: Db
I have the following class in my C# .NET 3.5 win forms app: class
I have the following classes: public class MyItems : List<MyItem> { ... } public
Given the following inheritance tree: I have a public class BaseForm : Form This
i have a winforms project, and i created a class on assembly A that
I have a Winforms application that is working correctly on various versions of windows
Suppose I have the following class: using System; using System.Collections.Generic; using System.Linq; using System.Text;
Technology: C#, Winforms The Background: I have the following container structure (Form containing a
I have a DataTable which is bound to datagridview (Winforms)... I use the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.