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

x
?
Solved

inheritance in VB

Posted on 1998-12-31
9
Medium Priority
?
394 Views
Last Modified: 2013-11-25
Are there inheritance features in VB like C++ or Java so that I can create subclasses that can inherit methods and properties from superclass?
0
Comment
Question by:kimsoy
[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
9 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1453404
Nope. But what do you want to do or know. Because a one word answer is not worth 100 points.

You have interfaces though in VB but no inheritence of code. Sorry.
0
 
LVL 3

Expert Comment

by:covington
ID: 1453405
The 'Implements' keyword will allow you to inherit the interface, but not the implementaion.
0
 
LVL 15

Accepted Solution

by:
ameba earned 400 total points
ID: 1453406
If you need inheritance in VB really badly, see this concept (grabbed NG posting)

--------------------
Here's how I get VB to 'behave' like an OOP:

First, create your base class as you normally do.  I include complete code
here - this is no empty interface class.

Next, create a subclass that will 'inherit' from the base.  add the
IMPLEMENTS line in the general dec area *AND* add an instance of the base,
too:

Implements BaseClass
Private objBaseClass as New BaseClass

Now expose the private interface of the base class as IMPLEMENTS requires.
but *also* add a public interface to match the private base class.  This
creates a 'published' version of the 'inherited' class (messy, but
useful...).

Now in each *public* member add code like this:
Private Sub BaseClass_MyMethod()
   ' add this code yourself
   objBaseClass.MyMethod
   '
End Sub

Yes, I'm delegating from the private class down to my local instance of the
BaseClass.

Finally, add an additional delegation from the public interface of SubClass
down to the private interface (truly a waste of time, but needed for type
management).

Public Sub MyMethod()
   ' add this code yourself
   BaseClass_MyMethod
   '
End Sub

You now have a subclass that has 'inherited' all the behavior of the base
class.  Any changes in code in teh base class will be reflected in the
subclass without any need to touch the sub class.

Of course you can do overrides in the sub class as needed and add new
properties and methods in the sub class to extend the base class.

Here's my key steps:
- add implements and get a local instance of the base class
- add a public interface to match the private interface
- delegate from the public interface to the private interface
- delegate from the private interface to the local instance of the base
class

The problem will all this is that its so danged tedious (and kludgy).  I
wrote myself an addin that does all this work for me, tho.  Now I just build
the base class, call the addin and tell it to build my subclasses.


MCA
Michael C. Amundsen, MCP, MVP

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:Ajoy
ID: 1453407
kimsoy,
 As told by many above, in VB there is no code inheritance. yes u can have interface inheritace thru the IMPLEMENTS keword
u can inherit the interfaces and write ur code there
regards,
ajoy
0
 

Author Comment

by:kimsoy
ID: 1453408
Are there any downsides that I have to keep in mind?
0
 

Author Comment

by:kimsoy
ID: 1453409
I forgot to thank ameba, you have saved a lot of time and efforts. Thanks again...
0
 
LVL 15

Expert Comment

by:ameba
ID: 1453410
kimsoy, if your SuperClass *definition* change, you are in trouble: Too many changes in your code, re-delegating code again...
You can simplify things, if you throw Implements and all its code, and use *only* instance of the base class, but you will loose type checking.
Some experts think inheritance is more trouble than benefits.
0
 

Author Comment

by:kimsoy
ID: 1453411
Here is the other way to do ingeritance that I got from other VB web site.  I didn't try this example yet, but it makes sence (I think...).

      'Class : clsStudent
      ' Contains a reference to a Person Object

      'private member variables
      Private oPerson as clsPerson
      Private iGrade as Integer

      Public Sub Create ( )
      'load this object and create the person object

           Set oPerson = New clsPerson

      End Sub

      Public Property Let Name ( sNewName as String)
      'set the person objects Name Property

           oPerson.Name = sNewName

      End Property

      Public Property Get Name ( ) as String
      'retreive the person objects Name Property

           Name = oPerson.Name

      End Property


How is it different from the answer I was given?
Which way is better (less trouble)?
   
0
 
LVL 15

Expert Comment

by:ameba
ID: 1453412
Yes, this is less trouble (without Implements).
I think, you should remove "Create" method and use:

Private Sub Class_Initialize()
    Set oPerson = New clsPerson
End Sub

Private Sub Class_Terminate()
    Set oPerson = Nothing
End Sub

Well, someone can say this is not Inheritance, but Delegating.
--

Implements can help if you have a Method that expects BaseClass as argument:

Public Function IsRetired(objItem as clsPerson) As Boolean
    If Left$(objItem.Name, 3) = "em." Then
        IsRetired = True
    End If
End Sub

Without Implements, this will produce type-mismatch error:
Set objStudent = New clsStudent
x = IsRetired(objStudent)

By adding Implements you can avoid this error.

But this works only one level deep: If you have clsEEStudent which implements clsStudent, you will not be able to use IsRetired method - clsEEStudent doesn't have Implements clsPerson line in the module :(
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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

704 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