• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 148
  • Last Modified:

Inherit seems to good to be true

Is there any downfall to inheriting a class instead of the class being passed around  in the New() sub
0
Hepen
Asked:
Hepen
  • 2
  • 2
  • 2
  • +3
1 Solution
 
LordWabbitCommented:
It would depend on what you where planning on doing, you're question is a bit vague btw.  Lobbing objects around is all good and well, but at the end of the day it will make your code messy and harder to debug.  Encapsulating certain basic functions and core tasks in base classes which get inherited makes the code neater and in the long run easier to maintain, also there would probably be less memory usage at the end of the day although the difference would probably be negligible unless the developer habitually did not clean up after himself.  If say for example you had a base class which would save and retrieve certain settings in the registry, then the inheriting class using this base class decides it wants to write an extra setting to the registry can override the base class method and write the extra setting into the registry before invoking the base classes registry update.  Trying stuff like that without inheritance etc. gets really messy after a while.  The key IMHO is determining what needs to go into the base class's.
0
 
SanclerCommented:
I'm not sure I understand the question.  But, so far as I do, the answer is "I don't think so".

You have a class called Widget.  You want some Black Widgets and some White Widgets.

Scenario A: you inherit from Widget into (1) BlackWidget and (2) WhiteWidget - hardcoding the appropriate, different color into each.

Scenario B: you code the New() method of Widget to accept a color argument and call New(Black) when you want BlackWidgets and New(White) when you want WhiteWidgets

Scenario C: you give your Widget class a Color property and, having created an instance of it with New you then call
   myWidget.Color = Black
or
   myWidget.Color = White

Whichever way you do it, each instance of the class/es is a separate object.  So, at this level anyway, I can't see what sort of "downfall" there might be.

What sort of thing did you have in mind?

Roger
0
 
Arthur_WoodCommented:
Inheritance only makes sense when the DERIVED class (that which is INHERITING from the base class) has an "IS A" relationship to the base class.

Consider a Base class MAMMAL and a derived Class DOG ( clear a DOG 'IS A' MAMMAL).   In this case, Inheritance makes a lot of sense, since much of the nature of the DOG class can be placed in the MAMMAL class, and only those aspects that make a DOG a DOG (and are different from say a PERSON - a PERSON is also a MAMMAL, but quite different froma DOG - for one thing , a PERSON has 2 legs, while a DOG has 4 legs).

But it would make abosolutely no sense whatsoever to try to create a BATTLESHIP class that inherits from a MAMMAL class - clearly a BATTLESHIP has absolutley nothing in common with a MAMMAL.

AW
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bob LearnedCommented:
Yeah, I'll jump on the bandwagon and say that this question doesn't make any sense :(

Bob
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I think he wants to access values from one class, inside another class, and has discovered that if you inherit from the first class then you can access them.

The purpose of the question would then be for us to contrast that approach with simply passing a reference to the first class into the second and list advantages/disadvantages of each.
0
 
Bob LearnedCommented:
~IM,
Hi, Mike, you must be a mind reader, or maybe you helped with a different question that would give you important insights here ;)

Bob
0
 
HepenAuthor Commented:
I have a class that performs asynchronous TCP.  Within those async TCP threads I have to use delegates in order to get data out of the thread and into my form.  I have so many delegates now that it is taking up a lot of space in my class.  Now I just made a seperate class called clsFormDelegates and I Inherit it when I need to use the delegates to get data from a thread into my form.

Is this the correct usage of Inheriting?
0
 
HepenAuthor Commented:
What if I were to just create an instance of clsFormDelegates and call each method like:

clsFormDelegates.UpdateCountInfo
clsFormDelegates.UpdateMessageHistory

Or if I inherited

UpdateCountInfo
UpdateMessageHistory

If I inherit it looks as if the above methods are part of the class which could throw some of my team members off.  They will be like where did these methods come from? Oh you inherited them? I see.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Take the Delegates out of the Class then...

Just make the Class raise events that notify the GUI of what is happening and to pass data out.

Let the FORM store and make the Delegates since it needs to update itself.

See this ridiculous example I came up with here:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21729609.html
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now