Setting up Classes

D

DeathSurfer

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.
 
C

Chip Pearson

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)
 
D

DeathSurfer

Preciate it dude.

Chip Pearson said:
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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top