tags:

views:

216

answers:

6

I am trying to do the following:

class Program
{
    static void Main(string[] args)
    {
        foo<baz> fooObject = new foo<baz>();
        AnotherClass<baz> _baz = new AnotherClass<baz>();
        _baz.testMethod(fooObject);
    }
}

public class AnotherClass<T> where T : bar
{
    public void testMethod(foo<T> dummy)
    {
        foobar = dummy;
    }

    private foo<T> foobar = null;
}

public class foo<T> where T : bar, new()
{
    public foo()
    {
        _t = new T();
    }

    private T _t;

}

public abstract class bar
{
    public abstract void someMethod();
    // Some implementation
}

public class baz : bar
{
    public override void someMethod()
    {
        //Implementation
    }
}    

And I get an error explaining that 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method. I fully understand why this must be, and also understand that I could pass a pre-initialized object of type 'T' in as a constructor argument to avoid having to 'new' it, but is there any way around this? any way to enforce classes that derive from 'bar' to supply parameterless constructors?

Fixed - I was missing a 'new()' constraint on AnotherClass().

A: 

Isn't the generic syntax new() rather than just new?

Also, you're doing this code in the middle of a class rather than in a function, which is sure to baffle it:

_t = new T();

Edit:

Ok, so after the syntax cleanup, I'm guessing the issue is that 'baz' does not have a parameterless constructor. I don't know of any way in which you can enforce this.

There is a similar question here, which may help out a little.

Fiona Holder
"In the middle of the class": this is just a field declaration with a initial value.
Stefan Steinegger
Fixed, thanks. My bad habit of half-pseudocode, half real code!
FrisbeeBen
+7  A: 

The correct syntax is

public class foo<T> where T : bar, new()
Gerrie Schenck
Sorry, I typed that in a rush.
FrisbeeBen
A: 

Like workaround - You can pass delegate creating the instance of a type you need:

  Func<T> creator = ...

  _t = creator();
Andrew Bezzub
+1  A: 

Your code compiles in VS2010.

You probably want to use interfaces instead:

public class foo<T> where T : ibar, new()
...

public interface ibar
{
    void someMethod();
}

public abstract class bar : ibar
{
    public abstract void someMethod();
    // Some implementation
}
devio
This generates the same issue for me...
FrisbeeBen
+1  A: 

With my edits to make it compile (eg override rather than overide), it compiles and runsin VS 2005 with no complaints. Please show us a short but complete program that actually demonstrates the problem you are having.

update:

AnotherClass also needs the new() condition in the where clause, since foo requires it.

AakashM
Updated to show complete problem that exhibits the issue.
FrisbeeBen
+2  A: 

Aren't you missing a new() constraint on your AnotherClass?

public class AnotherClass<T> where T : bar, new()

Without that VS2010 refuses to compile, with that it works just fine.

Hightechrider
Thanks, this seems to have fixed it!
FrisbeeBen