Inheriting Windows Control as Abstract Class

  • Thread starter Thread starter Mauro
  • Start date Start date
M

Mauro

Hi,

we'd like to define an abstract library class for a windows control, where
we specify a couple of abstract methods in order that they must be
overridden and implemented differently in each derived class.

We've tried creating the base abstract class and the derived class in the
main project but this leads to the following error appearing in the designer
view of the derived class:

"The designer must create an instance of "IamsPack.ctlIamsPackBase" but it
can't because the type is declared as abstract"

The funny thing is that the designer view displays fine when we first open
the solution, and then gives an error after we've run the application from
within Visual Studio.

I've read in another post that it's more stable to create the base class in
a separate library DLL and then add a reference in the main project. But
before I do that I'd like confirmation whether there is any problem with
making the base class abstract. Any feedback/suggestions would be greatly
appreciated.

Thanks,

Mauro Ciaccio
 
Chris,

thanks for that, which makes me feel once again sane...

As for your solutions, I like the interface one because it seems simpler to
implement. I'm not quite sure how you would implement the first approach.

As a short term solution I also have implemented virtual/overridable
functions, but there is no way to force the coder to implement an override
version in the derived class. I'm not sure I understand how/where you would
throw the System.NotImplementedException error. Could you provide a little
code snippet to illustrate?

Thanks,

Mauro

Chris Willis said:
Hey Mauro,

We had the exact same problem and we're unable to make our base classes
abtract and still use the designer for the derived classes. Looks like the
visual inheritance isn't quite perfect yet! [but this is Microsoft :) ]
What we did to work around the issue was to declare all methods that we
wished to be abstract as virtual functions. Then in the implementations in
the abse class, we throw the System.NotImplementedException with a text
message like "FunctionA() must be implemented by the derived class". You
could also define an interface that includes these methods and force all
classes that derive from it to implement the interface as a programming
standard. I realize that these are workarounds but they basically
accomplish the same task. Hope this works for you!

- Chris

Mauro said:
Hi,

we'd like to define an abstract library class for a windows control, where
we specify a couple of abstract methods in order that they must be
overridden and implemented differently in each derived class.

We've tried creating the base abstract class and the derived class in the
main project but this leads to the following error appearing in the designer
view of the derived class:

"The designer must create an instance of "IamsPack.ctlIamsPackBase" but it
can't because the type is declared as abstract"

The funny thing is that the designer view displays fine when we first open
the solution, and then gives an error after we've run the application from
within Visual Studio.

I've read in another post that it's more stable to create the base class in
a separate library DLL and then add a reference in the main project. But
before I do that I'd like confirmation whether there is any problem with
making the base class abstract. Any feedback/suggestions would be greatly
appreciated.

Thanks,

Mauro Ciaccio
 
Sure:

public interface IDerivedControl
{
void ThisMethod(string stuff);
}

public class BaseControl : System.Windows.Forms.UserControl
{
public BaseControl () {}
public void ThisMethod(string stuff)
{
throw new NotImplementedException("Derived class must implement
ThisMethod");
}
}

public class ADerivedControl : BaseControl, IDerivedControl
{
public override ThisMethod(string stuff)
{
Console.WriteLine("Monkey's ate my skateboard.");
}
}

This example is a combination of both techniques, using interfaces and base
class exceptions. As you can see, if a method is called on the derived
class that is not implemented, you get an exception. Using interfaces with
this approach is nice, since it allows a developer to know which methods to
override. You will need to let them know the purpose of this interface, as
the compiler will not complain about missing methods, since these methods
are implemented in the base class. Of course, all of this is a workaround,
and not exactly the best programming pratice, but it's the best I can think
of to work around the problem. Hope this works (and I hope the next version
of VS fixes this problem).

- Chris

Mauro said:
Chris,

thanks for that, which makes me feel once again sane...

As for your solutions, I like the interface one because it seems simpler to
implement. I'm not quite sure how you would implement the first approach.

As a short term solution I also have implemented virtual/overridable
functions, but there is no way to force the coder to implement an override
version in the derived class. I'm not sure I understand how/where you would
throw the System.NotImplementedException error. Could you provide a little
code snippet to illustrate?

Thanks,

Mauro

Chris Willis said:
Hey Mauro,

We had the exact same problem and we're unable to make our base classes
abtract and still use the designer for the derived classes. Looks like the
visual inheritance isn't quite perfect yet! [but this is Microsoft :) ]
What we did to work around the issue was to declare all methods that we
wished to be abstract as virtual functions. Then in the implementations in
the abse class, we throw the System.NotImplementedException with a text
message like "FunctionA() must be implemented by the derived class". You
could also define an interface that includes these methods and force all
classes that derive from it to implement the interface as a programming
standard. I realize that these are workarounds but they basically
accomplish the same task. Hope this works for you!

- Chris

Mauro said:
Hi,

we'd like to define an abstract library class for a windows control, where
we specify a couple of abstract methods in order that they must be
overridden and implemented differently in each derived class.

We've tried creating the base abstract class and the derived class in the
main project but this leads to the following error appearing in the designer
view of the derived class:

"The designer must create an instance of "IamsPack.ctlIamsPackBase"
but
it class
in
 
Back
Top