ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Class Hierarchy in VBA (https://www.excelbanter.com/excel-programming/385854-class-hierarchy-vba.html)

[email protected]

Class Hierarchy in VBA
 
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


NickHK

Class Hierarchy in VBA
 
Are you referring to using "Implements" ?

NickHK

wrote in message
oups.com...
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




[email protected]

Class Hierarchy in VBA
 
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.


[email protected]

Class Hierarchy in VBA
 
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?



Norman Yuan[_2_]

Class Hierarchy 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#....


wrote in message
ups.com...
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?





[email protected]

Class Hierarchy in VBA
 
Ok thanks for the info, then ill have to work without a class hierarchy


Blue Aardvark

Class Hierarchy in VBA
 
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!!)


NickHK

Class Hierarchy in VBA
 
I think you need to read up on how Implements/Interface/classes work.
Chack the help for the Implements key word.

NickHK

"Blue Aardvark" wrote in message
...
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!!)




Blue Aardvark

Class Hierarchy in VBA
 
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


"NickHK" wrote:

I think you need to read up on how Implements/Interface/classes work.
Chack the help for the Implements key word.

NickHK

"Blue Aardvark" wrote in message
...
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!!)






All times are GMT +1. The time now is 05:17 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com