inherit form and generics

C

CassioT

Hi. I want to create a base form class with a generic parameter.

public BaseForm<T> : Form
{}

public MyForm : BaseForm<MyType>
{}

The problem here is that the inherited form doesn't work in the visual
studio designer but the execution is perfect. Without the generic
parameter it works in the designer.

Is there a way to this or, does anyone has a better solution?

Thank you

Cassio Tavares
 
S

Smokey Grindle

the designer has to be able to instanciate the form itself for it to work in
the designer... if it cant do this, the form wont load
 
C

CassioT

So, I think the designer doesn't know what to pass in the generic
parameter.

I have a temporary solution but I don't like it. When I have to use
the designer,
I change the inheritance to System.Windows.Forms.Form. Is not a good
solution
because I can't add a grid to the base form and design it in the
inherited.
 
P

Peter Duniho

So, I think the designer doesn't know what to pass in the generic
parameter.

What generic parameter? You specify the generic parameter in your class
declaration. As long as you're trying to instantiate the derived type
rather than the generic type, I don't see how that's an issue.

Do you have a parameterless constructor for your derived type?

I understand the need for the designer to be able to instantiate the
object, but if you have a parameterless constructor, I would think that
would be sufficient. If you do and it still doesn't work, I don't have an
answer, but hopefully someone else who understands the designer better
could explain why the designer is still having trouble, even though
there's a parameterless constructor.

Pete
 
C

CassioT

Hi Pete. Yes, my constructors are parameterless. The classes are very
simple
and I already made the test. Commented all inner class code and tried
with generics
and without it. Only work without generics.

In my mind, the designer tries to instantiate the base class and fails
because it
doesn't know what to put in the generic parameter.

I hope you're right. Someone who understands the designer....

Cassio Tavares
 
P

Peter Duniho

Hi Pete. Yes, my constructors are parameterless. The classes are very
simple
and I already made the test. Commented all inner class code and tried
with generics
and without it. Only work without generics.

When you say you tested it "without generics", did you replace the generic
base class with a different concrete class derived from Form? Or did you
just replace it with Form itself?

I ask because it's not clear from your description exactly how you did the
test, and it could be that the problem you're having isn't actually
related to generics per se.
In my mind, the designer tries to instantiate the base class and fails
because it
doesn't know what to put in the generic parameter.

But that's what I don't understand. The base class isn't the generic
class. It's a fully-qualified class, just as concrete as any other
class. The designer doesn't need to know what to put in the generic
parameter; you've already put that there already in your class definition.
I hope you're right. Someone who understands the designer....

I hope so. :)

I apologize for not trying it myself. It's not like it would be all that
hard to test here, but I've been spending some time trying to wrap my head
around Apple's Cocoa programming environment, and I'm trying my best to
not start up Windows today. Too distracting, 'cause I wind up wasting a
lot of time messing around with stuff that's interesting but not
productive in the short-term. :)

Pete
 
C

CassioT

Sorry Pete if I was not clear enough. The base class is the generic
class.


public BaseForm<T> : Form
{}

public MyForm : BaseForm<MyType>
{}

MyForm is the form that doesn't open in the designer. But using...

public BaseForm : Form
{}

public MyForm : BaseForm
{}

it opens.

But you made me think. :)

The base form, has the generic parameter, and opens in the
designer!!!

Maybe there is one more thing that I didn't told you. They are in
different
assemblies.

I will do more tests. I will copy all the code to the same assembly to
see what
happens.

You don't need to apologize Pete. You're already doing too much
talking with me
here. And I never heard about Apple's Cocoa before. :)

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top