Need help implementing property of base class in derived class. (1 Viewer)

papadilbert

New member
Local time
Yesterday, 20:56
Joined
Nov 29, 2011
Messages
8
In my base class (clsBase) I have Public aProperty As String and the Let and Get skeleton property methods.

In the derived class (clsDerived) I coded Implements clsBase.
I coded the Let and Get property methods, prefixing them with clsBase_xxxx.
That works great.

I can't figure out how to write the code in the derived class to refer to the base class property aProperty.

When I compile I get the following error in the derived class:
"Object module needs to implement 'aProperty' for the interface 'clsBase'

Following the pattern for methods being prefixed with the name of the base class I tried coding:

Dim clsBase_aProperty as String

That didn't work.

I know how to implement a method. How do I implement a property?
 

papadilbert

New member
Local time
Yesterday, 20:56
Joined
Nov 29, 2011
Messages
8
I solved my problem with the wrong the method being executed but I haven't figured out how to reference the property contained in the base class.

Following the naming rule for derived methods, I tried coding
Private clsBase_aProperty

but I still get the same compile error.
 

MarkK

bit cruncher
Local time
Yesterday, 20:56
Joined
Mar 17, 2004
Messages
8,180
First of all, "base class" is perhaps a bit of misnomer. I'd expect to hear that used where there is an inheritance relationship, which is very different from implementing an interface, and VBA does not support inheritance.
You've created a class module that defines an interface that other modules need to implement, but that interface definition carries no implementation itself, so there should be nothing in the interface definition to refer to.
So, in your interface class, say it's named IPerson, maybe you would see a property Get and a property Let, like ....
Code:
property get FirstName  as string
end property
property let FirstName(svalue as string)
end property
These are stubs and provide no functionality.

Now, in every module that implements that class I would expect to see something like ...
Code:
implements IPerson

private m_fname as string

property Get IPerson_FirstName as string
  IPerson_FirstName = m_fname
end property
property Let IPerson_FirstName(svalue as string)
  m_fname = svalue
end property
... noting that all implementation exists in the class that implements the interface.
Hope this helps...
Mark
 

papadilbert

New member
Local time
Yesterday, 20:56
Joined
Nov 29, 2011
Messages
8
OK... a class that defines an interface.

And when that interface has a public property, that property must be identified in the implementation class. I'm asking how to code that. Your example shows how to code the methods. That I figured out yesterday.

Are you suggesting that interfaces cannot contain properties? I haven't read anything that states that. But then, I haven't been able to find how to define it in the implementation either.
 

MarkK

bit cruncher
Local time
Yesterday, 20:56
Joined
Mar 17, 2004
Messages
8,180
An interface is a class.
An interface may expose properties, functions, subs, and/or variables.
An example of what might appear in an interface is my first block of code above, which is indeed a property, but it could be a function or a sub or a public variable.

A class that implements an interface must expose members that exactly match all the public members of the interface. All parameters and return values and sub/function signatures must be exactly the same as those defined by the interface--and member names must be prefixed with the name of the interface. My second block of code demonstrates a class that implements the interface defined in my first block of code.

And when that interface has a public property, that property must be identified in the implementation class. I'm asking how to code that.
The code I posted does exactly that. The first block is a public property in an interface. The second block is a class that implements it defines how it works.

Your example shows how to code the methods. That I figured out yesterday.
My example shows how to code properties. A method is a behavior exposed by a function or a sub.

Are you suggesting that interfaces cannot contain properties?
No. My first example shows an interface that contains a property. My second example shows a class implementing that interface defining the functioning of that property and exposing it.

Making sense?
These are complicated topics.
Cheers,
 

papadilbert

New member
Local time
Yesterday, 20:56
Joined
Nov 29, 2011
Messages
8
A property denotes a storage location to store a value.

VBA OOP has decided to use the word "Property" as a qualifier but in reality they are Let and Get methods.

The first block does not contain a property. It contains 2 methods to Let and Get a property called "FirstName" that has not been defined. To define the property it would look like:

Public FirstName as String

When the property is coded, the compiler looks for an implementation of the public property. I am now more convinced that an interface cannot contain a property like the one above, but only methods.

I can deal with that. I just wanted to be sure that I wasn't missing something.

Thanks for taking the time to help me understand. It's a pleasure to find people like you who are willing to share their knowledge and talent!!
 

Users who are viewing this thread

Top Bottom