Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.net derived classes

Posted on 2006-07-12
12
Medium Priority
?
378 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1200 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 800 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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