Class Hierarchy in VBA

  • Thread starter Thread starter krsone21121983
  • Start date Start date
K

krsone21121983

Is it possible to create a class hierarchy in VBA? If so, how do I
tell my sub class which class it extends?

Thanks for you help
 
yes thanks, "Implements" seems to look good from what I read in the
example in the help file. It looks like "Implements" is used for
implementing Interfaces and extending classes.
 
Now I have another Question.

Lets say I have a class "Person" with the attributes "firstName" and
"lastName". This class has a method "print" which will return the
first and last name as a string.
Now I create a class "Employee" which implements "Person" and has an
additional attribute "salary". Now i also want to create a "print"
method for "Employee" but i dont want to do it like this:

public function print() {
print = Me.firstName & Me.lastName & Me.salary
}

instead i would like to do it like this:

public function print() {
print = super.print() & Me.salary
}

super is supposed to call the print method from the "Person" class,
since "Employee" implements "Person". However in vba i don't know how
to do this, because super doesnt work in vba. Whats the way to do this
in vba?
 
VB/VBA is not a full Object-oriented language, it does not support
Inheritance, as full Object-Oriented languages do, like C++, Java, C#,
VB.NET...

So, you cannot derive a new class ona base class. That is, you cannot first
create a "Person" class, and then derive an "Employee" class on top of it.
In VB/VBA, you only have limited Inheritance capability: Implements another
class: you define a class with properties and methods (usually, there is no
code inside the properties and methods' definitions), and define a class
that implement that interface class. A class can inmplement one or more
other classes according to your need.

Note, "Implements" a class is different from "Derive" from a base class. In
true Object-oriented language, when you subclass from a base class, it
automatically gets all proerties/methods the base class has. With
"Implements", you must write code to implement all exposed property and
method definitions in the interface class (hence the word "Implements"),
which is basically the same when you implements Interfaces in Java, C++,
C#....
 
Hello

Classes are a real pain in vba. But you could try doing something like this.
Lets say you want Class B to extend Class A. In the class B code write:

Implements ClassA

private super as ClassA

private sub Class_Initialize
super = new ClassA
end sub

So now classB has an internal copy of ClassA inside it and so you can make
references to that. Eg: super.print( )

(in theory it should work, but as I'm currently stuck on interfaces too then
I may not be the best person for advice!!)
 
I think you need to read up on how Implements/Interface/classes work.
Chack the help for the Implements key word.

NickHK
 
I did read up! I got that idea off a microsoft page on this website.
And if anyone else is reading this then I forgot the "set" command in my
code. It should have been:

private sub Class_Initialize
set super = new ClassA
end sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top