Solved

vb.net derived classes

Posted on 2006-07-12
12
373 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Duplicating a tab page content to another in TabControl control 11 44
C# Single Form 8 42
Help with exporting to excel 4 25
VB.Net Tranfer data between to VB.Net apps 8 25
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 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

13 Experts available now in Live!

Get 1:1 Help Now