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 9244999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:09:59+00:00 2026-06-18T09:09:59+00:00

I am trying to test a method – and getting an error: Cannot create

  • 0

I am trying to test a method – and getting an error:

Cannot create an instance of the variable type ‘Item’ because it does not have the new() constraint

Required information for below:

public interface IHasRect
{
    Rectangle Rectangle { get; }
}

Helper class:

class Item : IHasRect
{
    public Item(Point p, int size)
    {
        m_size = size;
        m_rectangle = new Rectangle(p.X, p.Y, m_size, m_size); 
    }
}

For the function to be tested, I need to instantiate an object…

public class SomeClass<T> where T : IHasRect

The test:

public void CountTestHelper<Item>() where Item : IHasRect
{
    Rectangle rectangle = new Rectangle(0, 0, 100, 100); 
    SomeClass<Item> target = new SomeClass<Item>(rectangle);            
    Point p = new Point(10,10);
    Item i = new Item(p, 10);      // error here        
    ...
}
[TestMethod()]
public void CountTest()
{
    CountTestHelper<Item>();
}   

I am trying to understand what this error means, or how to fix it, by reading http://msdn.microsoft.com/en-us/library/d5x73970.aspx and http://msdn.microsoft.com/en-us/library/x3y47hd4.aspx – but it doesn’t help.

I don’t understand this error – I have already constrained the “SomeClass” to be of type. I cannot constrain the entire Test class (the unit test class generated by Visual Studio, which contains all the tests) – I will get a number of other errors otherwise. The Item class doesn’t have any template…

  • 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-06-18T09:10:00+00:00Added an answer on June 18, 2026 at 9:10 am

    The Item in the line:

    Item i = new Item(p, 10);
    

    refers to the generic type parameter Item of the CountTestHelper method, not the class Item. Change the generic parameter name e.g.

    public void CountTestHelper<TItem>() where TItem : IHasRect
    {
        Rectangle rectangle = new Rectangle(0, 0, 100, 100); 
        SomeClass<TItem> target = new SomeClass<TItem>(rectangle);            
        Point p = new Point(10,10);
        Item i = new Item(p, 10);    
        ...
    }
    

    alternatively you can fully qualify the name of the Item class you want to create:

    public void CountTestHelper<Item>() where Item : IHasRect
    {
        Rectangle rectangle = new Rectangle(0, 0, 100, 100); 
        SomeClass<Item> target = new SomeClass<Item>(rectangle);            
        Point p = new Point(10,10);
        SomeNamespace.Item i = new SomeNamespace.Item(p, 10);  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm am trying to test wcf method in C#. method return type is ilist.
I am trying to create a unit test for a method that takes a
Specifically, I'm trying to create a unit test for a method which requires uses
Im trying to test a create method that has a call to an external
I am trying to test that a method to load a UI matrix is
I'm trying to test a model method using Play Framework 2.0 and Specs2. Global.scala
I'm trying to test a presenter method using a Capybara RSpec matcher. Lets say
I'm trying to test an Order entity method called AddItem and I'm trying to
I'm trying to unit test a private method that I have attached to my
My test is trying to assert that a certain dal method was called, with

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.