Jarod Posted April 10, 2003 Posted April 10, 2003 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, Quote Jarod
*Gurus* divil Posted April 10, 2003 *Gurus* Posted April 10, 2003 What error do you get, and at exactly what point is it thrown? When you try to create your controls on the form? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Jarod Posted April 10, 2003 Author Posted April 10, 2003 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 Quote Jarod
*Gurus* divil Posted April 10, 2003 *Gurus* Posted April 10, 2003 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? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Jarod Posted April 10, 2003 Author Posted April 10, 2003 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 ? Quote Jarod
Jarod Posted April 10, 2003 Author Posted April 10, 2003 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 Quote Jarod
*Gurus* divil Posted April 10, 2003 *Gurus* Posted April 10, 2003 Oh, I see what you mean now. No, you can't use the designer to design a control that inherits from an abstract class. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted April 10, 2003 *Experts* Posted April 10, 2003 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 Quote "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
Cywizz Posted April 10, 2003 Posted April 10, 2003 vb version: #If Not DEBUG Then Public MustInherit Class myTextBoxBase #Else Public Class myTextBoxBase #End If Cheers! Quote Howzit??
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.