Solved

vb.net derived classes

Posted on 2006-07-12
12
377 Views
Last Modified: 2012-05-05
I am going to have 2 derived classes with a few common functions (commonFunction1, and commonFunction2) with some functions that are not the same.

Would it be best to have a 2nd level of derived classes, or put my code in another place.

so
mainClass
helperDerivedClass (derived from mainClass)
specialDerivedClass1 (derived from helperDerivedClass )
specialDerivedClass2 (derived from helperDerivedClass )

just for a few functions, or is there another place I could put just these common class specific functions?



0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 21

Expert Comment

by:mastoo
ID: 17091524
That would be reasonable as long as:

1. specialDerivedClass1 is a helperDerivedClass
2. specialDerivedClass2 is a helperDerivedClass
3. specialDerivedClass1 is not a specialDerivedClass2 and vice versa
0
 

Author Comment

by:jackjohnson44
ID: 17091562
Is there a way to just have a few common functions?
I would rather do that.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17091636
Depending on what you are doing, you can create a helper class that contains Public Shared methods.

However, if the methods you need to implement are specific to your classes, you should create them as you indicate. You should probably also declare helperDerivedClass as MustInherit.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 21

Expert Comment

by:mastoo
ID: 17091781
Ditto Chaosian.  If they are private functions, helper class is fine.  If not, you likely want inheritance or interfaces delegating to a helper class.
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17091837


Question :


You have the setup :
Main
HelperDerived (derived from main)
Special1 (derived from helper)
Special2 (derived from helper)


Is there something in "HelperDerived" that couldnt go in "Main"?

In the main class you could define those "common" functions and inherit from it.

Class Main
 
  public function commonfunction1(byval something as integer) as string
     'logic / etc
  end function

  public function commonfunction2(byval something as integer) as string
       'logic / etc  
  end function

end class

public class special1
inherits main
public function special1function(byval something as string) as integer
  'logic
end function
end class

public class special2
inherits main
public function special2function(byval something as string) as integer
  'logic
end function
end class


I would put the "common" functions in whatever base class you will be deriving the "special" classes from, this simplifies your model.
0
 

Author Comment

by:jackjohnson44
ID: 17091873
the main class can't have the functions in question.
There are many more classes than I indicated.

mainClass
helperDerivedClass (derived from mainClass)
specialDerivedClass1 (derived from helperDerivedClass )    'these have common functions function1
specialDerivedClass2 (derived from helperDerivedClass )

specialDerivedClass3 (derived from mainClass)
specialDerivedClass4 (derived from mainClass)
specialDerivedClass5 (derived from mainClass)

each class will contain an instance of function1, but it will be different for some and common to some.


"If not, you likely want inheritance or interfaces delegating to a helper class" - how do I do this?
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17091936
In your main class, put function1 with the overrideable keyword...

Public Overrideable Function Function1 As Integer
' your common function 1 goes here
End Function

This way, all your classes will inherit the common function1.

Then, in the special classes, add

Public Overrides Function Function1 As Integer
' Your special function 1 goes here
End Function
0
 

Author Comment

by:jackjohnson44
ID: 17092022
so helper classes are the way to go?
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 300 total points
ID: 17092063
In this case, yes.

Although, if special1 and special2 have different implementations of function1, you could remove the helper class, and just use the overrides in special1 and special2 but not in special3-5.
0
 
LVL 8

Assisted Solution

by:Shakti109
Shakti109 earned 200 total points
ID: 17092070

You can use either method that makes the most sense to you as they are equal.

Wether you put the functions in the main class and override them, OR, put them in helper classes and inherit separately from them, you will get the same result.

The only caveat is that with each level of inheritance you create, you also create troublehsooting complexity.

0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17092103
There's no point to adding the complexity of a helper class that has all functions overridden by all children... the functionality will be the same, but you add maintenance complexity.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17092120
Glad to be of assistance :)
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question