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

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

sdday81

New Member
Joined
Feb 28, 2014
Messages
25
I'm guessing most people here don't take advantage of the class modules. Oh well, I'll figure it out.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,305
Office Version
  1. 365
Platform
  1. Windows
Can you explain exactly what you mean, especially the 'relationship' bit in the title?
 

sdday81

New Member
Joined
Feb 28, 2014
Messages
25
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.
 

=ODIN=

Active Member
Joined
Dec 3, 2009
Messages
289

ADVERTISEMENT

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
 

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,029
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:

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,029
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.
 

Forum statistics

Threads
1,144,625
Messages
5,725,365
Members
422,621
Latest member
dfrare

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
Top