Class Module Has A Relationship

sdday81

New Member
Joined
Feb 28, 2014
Messages
25
Can I have class modules that contain other class modules as members of that class, like other OOP languages? If so how and when would the other class member be instantiated? Or would I have to Set it after creating and setting the object that contains it? Does that make sense?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I'm guessing most people here don't take advantage of the class modules. Oh well, I'll figure it out.
 
Upvote 0
Can you explain exactly what you mean, especially the 'relationship' bit in the title?
 
Upvote 0
In OOP there are relationships called is-a and has-a. Is-A deals with inheritance. Let's say I have an Animal class and a Dog Class that inherits from Animal class. In this case Dog is-a Animal.

Code:
' Is-a relationship
Class Animal
{
}
Class Dog: Public Animal
{
}

With a Has-A relationship we are referring to containment (one object containing another). For example I would have a car class and a engine class. The car class would contain an instance of the engine class.

Code:
' Has-a relationship
Class Engine
{
}
Class Car
{
     Engine e;
}

My question is can we use has-a relationship designs using class modules in VBA like other OOP languages. Can we have a class module that contain an instance of another class module.

Thank You.
 
Upvote 0
I would think what you are saying would be possible. Inside one class module, I would think you could "set" an object to refer to your other class, so that when instantiated you would get a related instance of the subclass.

what is your need for such a relationship? for most of us, the existing classes in vba are all we need. i have only ever used a custom class to capture application events.

this link shows a little about classes in vba
Using Classes in VBA | Expert Zone
 
Upvote 0
LOL! It's a little presumptuous of you to claim that "for most of us the existing classes in vba are all we need," isn't it? I routinely develop solutions that require multiple custom classes. And, I know there are several others who have the skills to use OOP in a fairly advanced manner -- whether they do or not I cannot say.

Of course, I guess one could argue that I/we represent an outlier of the mrexcel population and, so, your statement might still be valid. {grin}

I would think what you are saying would be possible. Inside one class module, I would think you could "set" an object to refer to your other class, so that when instantiated you would get a related instance of the subclass.

what is your need for such a relationship? for most of us, the existing classes in vba are all we need. i have only ever used a custom class to capture application events.

this link shows a little about classes in vba
Using Classes in VBA | Expert Zone
 
Last edited:
Upvote 0
The first, i.e., inheritance, is not supported by VBA. The second is.
In OOP there are relationships called is-a and has-a. Is-A deals with inheritance. Let's say I have an Animal class and a Dog Class that inherits from Animal class. In this case Dog is-a Animal.

Code:
' Is-a relationship
Class Animal
{
}
Class Dog: Public Animal
{
}

With a Has-A relationship we are referring to containment (one object containing another). For example I would have a car class and a engine class. The car class would contain an instance of the engine class.

Code:
' Has-a relationship
Class Engine
{
}
Class Car
{
     Engine e;
}

My question is can we use has-a relationship designs using class modules in VBA like other OOP languages. Can we have a class module that contain an instance of another class module.

Thank You.
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top