Can you have a generic-type Windows form?

M

Michael Rodriguez

Is it possible to have a Windows form that is declared as a generic type,
i.e.:

public partial class DataEntry<T, C> : DataEntryBase
where T: ...
where C: ...

I tried that, but it chokes on this line:

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(DataEntry));

I can make that compile by doing this:

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(DataEntry<,>));

But then the screen won't load, it says it can't find the resource file.

Any ideas?

TIA,

Mike Rodriguez
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

Instead of doing:

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(DataEntry));

Or:

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(DataEntry<,>));

You should do:

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(DataEntry<T, C>));

Which will give you the correct type of your parameterized class.

Hope this helps.
 
M

Michael Rodriguez

Hi Nicholas.

Thanks for the quick reply! I actually had already tried that. It
compiles, but then the screen crashes on "Could not find any resources
appropriate to the specified culture...".

I read where the resource file is named after the first class it finds in
the file. It seems to be choking on my generic class definition, so I
majorly hacked it like this:

// This is only here so the compiler can find the resource file.
class DataEntry
{ }

public partial class DataEntry<T, C> : DataEntryBase
where T: Csla.Core.IEditableCollection, IList<C>
where C: Csla.Core.IEditableBusinessObject

That seems to work, but I'm nervous about what potential future problems
could arise. Do you think this will be ok?

Thanks!

Mike


Nicholas Paldino said:
Michael,

Instead of doing:

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(DataEntry));

Or:

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(DataEntry<,>));

You should do:

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(DataEntry<T,
C>));

Which will give you the correct type of your parameterized class.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Michael Rodriguez said:
Is it possible to have a Windows form that is declared as a generic type,
i.e.:

public partial class DataEntry<T, C> : DataEntryBase
where T: ...
where C: ...

I tried that, but it chokes on this line:

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(DataEntry));

I can make that compile by doing this:

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(DataEntry<,>));

But then the screen won't load, it says it can't find the resource file.

Any ideas?

TIA,

Mike Rodriguez
 
J

John B

Michael said:
Hi Nicholas.

Thanks for the quick reply! I actually had already tried that. It
compiles, but then the screen crashes on "Could not find any resources
appropriate to the specified culture...".

I read where the resource file is named after the first class it finds in
the file. It seems to be choking on my generic class definition, so I
majorly hacked it like this:

// This is only here so the compiler can find the resource file.
class DataEntry
{ }

public partial class DataEntry<T, C> : DataEntryBase
where T: Csla.Core.IEditableCollection, IList<C>
where C: Csla.Core.IEditableBusinessObject

That seems to work, but I'm nervous about what potential future problems
could arise. Do you think this will be ok?
<...>

Hi Mike,
We needed a generic form too, it also would not display in design mode
anymore.
We could hack at the generated code to make it show but every time we
made a change the code would just get regenerated so it was no good :(

We gave up on it in the end :(

JB
 
C

Christof Nordiek

Hi Michael,

i supppose the problem is, that the designer can't create a generic class,
(though, it shuold warm you).

You could design a non-generic baseclass and then derive from it a generic
class (without th Forms-Designer).
 

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