Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / January 2005

Tip: Looking for answers? Try searching our database.

Need help: about OOP inheritance/abstract class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tee - 27 Jan 2005 03:06 GMT
Hi,

I have a base usercontrol with a method (blank method, no code), I have
another few usercontrols that will inherit this base usercontrol, but I want
to force all the usercontrol that inheriting this base usercontrol to
override the method with its own code. How do I do it?

I have tried to make the base usercontrol an abstract class (mustinherits +
mustoverrides), this works, but all the usercontrols that inherit it will
not able to open in VS designer because of inherting an abstract class.

I have also tried to use interface, this works as well, but I can't force
those usercontrols that inherit base usercontrol MUST have the interface.

Anyone has a solution for this?

Thanks,
Tee
Elementary Penguin - 27 Jan 2005 03:11 GMT
> Hi,
>
> I have a base usercontrol with a method (blank method, no code), I have
> another few usercontrols that will inherit this base usercontrol, but I
> want to force all the usercontrol that inheriting this base usercontrol to
> override the method with its own code. How do I do it?

If you are going to /force/ them to override it ( polymorphism ) why have it
in the first place?

You should define an *interface* with a stub method and force them to
implement it.

I do this in one of my web service applications.  There are two interfaces
and a base abstract class.   The interface accounts for methods that must
be implemented in the derived classes, but which have no default method in
the abstract class.   So each derived class has one abstract class and two
interfaces to inherit from.

Signature

http://texeme.com
Textcasting Technology
Incognito Blog
http://incognito.texeme.com

Tee - 27 Jan 2005 03:35 GMT
Hi,

> You should define an *interface* with a stub method and force them to
implement it.
Is stub method a keyword?

> I do this in one of my web service applications.  There are two
...........
What I am doing is a usercontrol, what I meant "I can't open the usercontrol
in VS designer" is because when it opens up the form/usercontrol, it
actually called it as "new", and abstract class can't be declared as new"

Thanks.

> > Hi,
> >
[quoted text clipped - 14 lines]
> the abstract class.   So each derived class has one abstract class and two
> interfaces to inherit from.
Elementary Penguin - 27 Jan 2005 06:25 GMT
> Is stub method a keyword?

Sorry, bad choice of words.

Have you ever used the /interface/ keyword though?

You define methods, but just in terms of the input parameters.

Then, its up to the inheriting classes to implement.   Similar to, but not
exactly, to an abstract class.

And there's no *default* implementation.

> What I am doing is a usercontrol, what I meant "I can't open the
> usercontrol in VS designer" is because when it opens up the
> form/usercontrol, it actually called it as "new", and abstract class can't
> be declared as new"

No, but you can create a class that instantiates the user control in the
constructor, or that inherits it, and inherit the interfaces.

> Thanks.
>
[quoted text clipped - 26 lines]
>> Incognito Blog
>> http://incognito.texeme.com

Signature

http://texeme.com
Textcasting Technology
Incognito Blog
http://incognito.texeme.com

Tee - 27 Jan 2005 09:22 GMT
> Have you ever used the /interface/ keyword though?
Yes, I know what is it and used it before. Since I can't force a class to
implements an interface, I don't think this is what I am looking for.

> No, but you can create a class that instantiates the user control in the
> constructor, or that inherits it, and inherit the interfaces.
Sorry I am somehow a bit lost in this, I can't figure it out. Can you tell
me how to instantiates user control in constructor?
I think this should be what I need, greatly appreciate if you could
elaborate on this.

Thanks,
Tee

> > Is stub method a keyword?
>
[quoted text clipped - 47 lines]
> >> Incognito Blog
> >> http://incognito.texeme.com
Elementary Penguin - 28 Jan 2005 16:56 GMT
>> Have you ever used the /interface/ keyword though?
> Yes, I know what is it and used it before. Since I can't force a class to
[quoted text clipped - 4 lines]
> Sorry I am somehow a bit lost in this, I can't figure it out. Can you tell
> me how to instantiates user control in constructor?

I used this trick when I was trying to get an XmlDocument to load at runtime
(static), by instantiating it.   Normally, I had to run the .Load method --
but I can't run a method when I declare, I have to declare, then .Load.

So, I created a new class with a constructor that did the load.

In my calling class

CustomXmlDocClass myXml = new CustomXmlDocClass()

Then in the constructor of the instantiated class:

CustomXmlDocClass
{

       public XmlDocument xd;

       CustomXmlDocCalss()
       //the constructor
       {
               XmlDocument _xd = new XmlDocument();
               _xd.Load("whatever.xml");
               xd = _xd;
       }

}

Now myXml is loaded with whatever.xml at run time.

> I think this should be what I need, greatly appreciate if you could
> elaborate on this.
[quoted text clipped - 66 lines]
>> Incognito Blog
>> http://incognito.texeme.com

Signature

Texeme
http://texeme.com

Olorin - 27 Jan 2005 03:13 GMT
I haven't tried, but have you tried to use inheirtance AND interface
together?

i.e.
* base class with stuff that should be inherited as-is
* interface with the definition of the method that the inheriting
classes should implement/override
* inheriting/implementing classes: they inherit from the base class,
AND they implement the interface.

The children should then get all that the base class provides and Be
Forced to implement the method(s) defined in the interface.
HTH,
F.O.R.
Tee - 27 Jan 2005 03:39 GMT
I have tried it.

> The children should then get all that the base class provides and Be
> Forced to implement the method(s) defined in the interface.
> HTH,
> F.O.R.

The base class that implements the interface will automatically has the
method in the class (just an empty method, no code).
And yes, the children get all the base class provided, and they inherit the
empty method as well ... the problem is it won't force the children to
overrides it.

Thanks.

> I haven't tried, but have you tried to use inheirtance AND interface
> together?
[quoted text clipped - 10 lines]
> HTH,
> F.O.R.
Bruce Wood - 27 Jan 2005 05:24 GMT
This is not just a problem with user controls; it is also a problem
with inheriting Forms.

You cannot use an abstract base class for the reasons you mentioned.
There is no way around it at present.

The best you can do is write the methods in the base class to throw a
NotImplementedException. Then if the child class does not override them
it will blow up at run time. There is no way to enforce the override
via a compile-time check.
Nick Hall - 27 Jan 2005 12:15 GMT
> This is not just a problem with user controls; it is also a problem
> with inheriting Forms.
[quoted text clipped - 6 lines]
> it will blow up at run time. There is no way to enforce the override
> via a compile-time check.

Well there is one workaround which I've used.  You can use use compile time
constants to declare your class such that the MustInherit attribute is only
enforced in Release builds e.g.

#If Debug then
   Public MyClass
#Else
   Public MustInherit MyClass
#End if
...

Any methods that should be overridden can be given a default implementation
in the debug build (to write a message to the console or throw an exception)
e.g.

...
#If Debug then
   Public Sub MethodThatMustBeOverriden()
       Debug.Fail("Method not overridden")
   End Sub
#Else
   Public MustOverride Sub MethodThatMustBeOverriden()
#End if

This has worked OK for me in the past.

Hope this helps,

Nick Hall
Bruce Wood - 27 Jan 2005 18:38 GMT
Very clever. Perhaps I'll try that on my projects next time.
Tee - 28 Jan 2005 07:41 GMT
Very great idea, thanks!

> > This is not just a problem with user controls; it is also a problem
> > with inheriting Forms.
[quoted text clipped - 36 lines]
>
> Nick Hall
Olorin - 27 Jan 2005 12:43 GMT
> > The children should then get all that the base class provides and Be
> > Forced to implement the method(s) defined in the interface.
[quoted text clipped - 6 lines]
> empty method as well ... the problem is it won't force the children to
> overrides it.

I didn't say that the base class should implement the interface ;-)
ANd, yes I now realize that the solution I proposed would have the draw
back of the
base class not implementing the method(s) in your interface. SO it
might or might not work for you.

HTH,
F.O.R.
Dennis Myr?n - 27 Jan 2005 08:37 GMT
Why not just(shortened):

abstract class Abstract
{
 public abstract void MustOverrideThisAbstractMethod ( ) ;
}

class Concrete : Abstract
{
 override public void MustOverrideThisAbstractMethod ( )
{
   //Implement something here.
}

}

Declaring a method as abstract within an abstract class means the base class
will demand an implementation of that method from the concrete derived
class.

Signature

Regards,
Dennis JD Myr?n
Oslo Kodebureau

> Hi,
>
[quoted text clipped - 16 lines]
> Thanks,
> Tee
Tee - 27 Jan 2005 09:06 GMT
Hi Dennis,

See my previous post:
>I have tried to make the base usercontrol an abstract class (mustinherits +
mustoverrides), this works, but all the usercontrols that inherit it will
not able to open in VS designer because of inherting an abstract class.

This just won't work on forms or usercontrols ... it won't effect runtime,
but it's trouble enough if you can't open it in VS Designer.

Thanks,
Tee

> Why not just(shortened):
>
[quoted text clipped - 36 lines]
> > Thanks,
> > Tee
Dennis Myr?n - 27 Jan 2005 09:17 GMT
Sorry, i should probably read the posts fully before replying them.

I had a bunch of webforms once which all needed common extra features in
order
to provide the functionality of a step in a wizard.
There was no way i could force those webforms to implement my custom
interface
in addition to inherit System.Web.UI.Page.
But i myself though, knew which ones of them should provide that
functionality,
and had them implement that interface.
class WizardStep1 : System.Web.UI.Page, IWizardStep

Signature

Regards,
Dennis JD Myr?n
Oslo Kodebureau

> Hi Dennis,
>
[quoted text clipped - 55 lines]
>> > Thanks,
>> > Tee

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.