Solved

vb.net derived classes

Posted on 2006-07-12
12
371 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
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 21

Expert Comment

by:mastoo
Comment Utility
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
Comment Utility
Is there a way to just have a few common functions?
I would rather do that.
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
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
 
LVL 21

Expert Comment

by:mastoo
Comment Utility
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
Comment Utility


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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
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
Comment Utility
so helper classes are the way to go?
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 300 total points
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
Glad to be of assistance :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now