Solved

vb.net derived classes

Posted on 2006-07-12
12
376 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

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!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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