Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

In some case, I want to make my own custom control. And in some cases, I want to have first an abstract class to group global code like :

public mustInherit Class myTextBoxBase
  inherits TextBox

...
end class

public Class myTextBox1
  inherits myTextBoxBase
...
end class

public Class myTextBox2
  inherits myTextBoxBase
...
end class

With that structure, my classes work fine but I have a designer error while looking in myTextBox1 and myTextBox2 designer because my base class is abstract and so cannot be built.

This can be annoying if I want to add other graphic component in a SubClass.

 

Is there a way of avoiding that error (and letting the base class as abstract?)

 

Thanks,

Jarod
Posted

The error is when I try to see the designer of my Object (at design time)

The error is:

"An error occurred while loading the document. Fix that error and try loading the document again. The error message follows: The designer must create an instant of type 'myTextBoxBase' but it can't because the type is declared as abstract"

 

It is not a critical error because I can add my new components directly in the code instead of in the designer, but I just wonder if there is a way to have an abstract base class (for a component) and being able to work in the subclass designer

Jarod
  • *Gurus*
Posted

I have had no problems doing this in the designer. I just tried deriving two controls from my own class which inherits from UserControl, and I can create both of them in the designer just fine. It shouldn't be trying to create an instance of the base class, it should be trying to create an instance of one of your derived ones.

 

Do your derived classes have a parameterless constructor the designers can call?

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

And when it create an instance of the derived class, it has to create an instance of the base class. (well, not really an instance, but you have the constructor chaining) That's the normal process for inheritance.

And here the base class is "MustInherit" and so can't be instantiated.

 

In your test, Have you put your base class (inheriting from UserControl) as MustInherit ?

Jarod
Posted
The problem doesn't appear when you add the Custom control to a form for example, but when you open the designer of the custom control
Jarod
  • *Experts*
Posted

You might try wrapping the abstract declaration with "#if !DEBUG" and use "#else" for the non-abstract version. This way, in DEBUG mode you can use the designer because your form is not abstract, and when you compile in Release it is. You may need to change it to abstract while in Debug mode for some extra testing (since making it abstract may cause problems if you didn't code it right).

 

Here's some C# code - I don't know the VB.NET syntax for "#if DEBUG" or which keywords make the declaration abstract (hopefully someone can translate this):

#if !DEBUG
public abstract class Form1 : System.Windows.Forms.Form
#else
public class Form1 : System.Windows.Forms.Form
#endif

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...