ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Setting up Classes (https://www.excelbanter.com/excel-programming/406118-setting-up-classes.html)

DeathSurfer

Setting up Classes
 
Duuudes:

I have a question about setting up classes. I currently have a situation
that I need to set up a class that contains properties then those properties
need to have properties. For ex:

I've set a class up called Shapes. The Shapes object then has multiple
shapes under it, Shapes.Square, Shapes.Circle, Shapes.Rectangle, etc.

I want to be able to get the color of a certain shape such as:
Shapes.Square.Color

I know how to set up the properties one level deep in a class with the
property let and property get procedures, but don't understand the syntax of
how to give a property it's own properties, if that makes sense. Do I need to
set up classes for each shape then associate that back to the Shapes object
somehow?

Thanks.


Chip Pearson

Setting up Classes
 
Try something like the following:

'[Class CCircle]
Public Radius As Double

Public Function Area() As Double
Area = 3.1415 * (Radius ^ 2)
End Function

'[Class CSquare]
Public SideLength As Double

Public Function Area() As Double
Area = SideLength ^ 2
End Function

'[Class CShape]
Public Function CircleObj() As Ccircle
Set CircleObj = New Ccircle
End Function

Public Function SquareObj() As CSquare
Set SquareObj = New CSquare
End Function

'[Module1]
Sub AAA()
Dim SH As CShape
Dim SQ As CSquare
Dim SC As Ccircle

Set SH = New CShape
Set SQ = SH.SquareObj
SQ.SideLength = 10
Debug.Print SQ.Area
End Sub

Depending on how complicated you want to go, you could also write your
CCircle and CSquare classes to Implement the CShape interface, which would
allow something like

Dim SH As CShape
Set SH = New CSquare


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2008
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


"DeathSurfer" wrote in message
...
Duuudes:

I have a question about setting up classes. I currently have a situation
that I need to set up a class that contains properties then those
properties
need to have properties. For ex:

I've set a class up called Shapes. The Shapes object then has multiple
shapes under it, Shapes.Square, Shapes.Circle, Shapes.Rectangle, etc.

I want to be able to get the color of a certain shape such as:
Shapes.Square.Color

I know how to set up the properties one level deep in a class with the
property let and property get procedures, but don't understand the syntax
of
how to give a property it's own properties, if that makes sense. Do I need
to
set up classes for each shape then associate that back to the Shapes
object
somehow?

Thanks.



DeathSurfer

Setting up Classes
 
Preciate it dude.

"Chip Pearson" wrote:

Try something like the following:

'[Class CCircle]
Public Radius As Double

Public Function Area() As Double
Area = 3.1415 * (Radius ^ 2)
End Function

'[Class CSquare]
Public SideLength As Double

Public Function Area() As Double
Area = SideLength ^ 2
End Function

'[Class CShape]
Public Function CircleObj() As Ccircle
Set CircleObj = New Ccircle
End Function

Public Function SquareObj() As CSquare
Set SquareObj = New CSquare
End Function

'[Module1]
Sub AAA()
Dim SH As CShape
Dim SQ As CSquare
Dim SC As Ccircle

Set SH = New CShape
Set SQ = SH.SquareObj
SQ.SideLength = 10
Debug.Print SQ.Area
End Sub

Depending on how complicated you want to go, you could also write your
CCircle and CSquare classes to Implement the CShape interface, which would
allow something like

Dim SH As CShape
Set SH = New CSquare


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2008
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


"DeathSurfer" wrote in message
...
Duuudes:

I have a question about setting up classes. I currently have a situation
that I need to set up a class that contains properties then those
properties
need to have properties. For ex:

I've set a class up called Shapes. The Shapes object then has multiple
shapes under it, Shapes.Square, Shapes.Circle, Shapes.Rectangle, etc.

I want to be able to get the color of a certain shape such as:
Shapes.Square.Color

I know how to set up the properties one level deep in a class with the
property let and property get procedures, but don't understand the syntax
of
how to give a property it's own properties, if that makes sense. Do I need
to
set up classes for each shape then associate that back to the Shapes
object
somehow?

Thanks.




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

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