Properties in a class

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,404
I was of the understanding the syntax for a class is as follows:

You create a private variable and then have public let / get properties to pass in / retrieve the data, like:

Code:
Private pSomeProperty As Integer

Property Get SomeProperty As Integer

SomeProperty = pSomeProperty

End Property

Property Let SomeProperty (p As Integer)

pSomeProperty = p

End Property


I came across this code:

Code:
Option Explicit

Private PieChart As Chart           'the chart we're creating
Private ThisSheet As Worksheet 'the worksheet we're on

Public Property Let DataRange(ThisRange As String)

    PieChart.SetSourceData ThisSheet.Range(ThisRange), PlotBy:=xlRows

End Property

Public Property Let DataPointToExplode(ThisPoint As Integer)

    'explode nth point on first and only data seriesr

PieChart.SeriesCollection(1).Points(ThisPoint).Explosion = 15

End Property

Public Property Let Title(ThisTitle As String)

    'the chart has a title - set its text

PieChart.HasTitle = True

With PieChart.ChartTitle

.Characters.Text = "Peruvian GDP per head ($)"
.Border.Weight = xlHairline
.Shadow = True

End With

End Property

Private Sub Class_Initialize()

    'store which worksheet we are on

Set ThisSheet = ActiveSheet

'create the new chart, and set some basic things about it

Set PieChart = Charts.Add

'what sort of chart this is

PieChart.ChartType = xlPie

'where it is (separate sheet)

PieChart.Location Where:=xlLocationAsNewSheet

End Sub

Sub PrintPreview()

    PieChart.PrintPreview

End Sub

How come DataRange, DataPointToExplode and Title all have a Let Property but no corresponding private variable?

Thanks
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,824
Office Version
  1. 2019
Platform
  1. Windows
Well, this is generally something you will just want to explore and test. This class looks like in general it is attempting to encapsulate a chart, so the main private variable is the private pie chart variable. The property here is setting the data source for the pie chart. I would say the general effect is that you are hiding the pie chart and then exposing only a subset of the pie charts properties and methods -- anything belong to the private pie chart variable that is not exposed as a class method or property becomes essential inaccessible. It might be a little strained as there may not be any particular reason to hide a pie chart this way but it exemplifies the concept of encapsulation which is one of the hallmarks of object-oriented programming using classes.
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,404
Well, this is generally something you will just want to explore and test. This class looks like in general it is attempting to encapsulate a chart, so the main private variable is the private pie chart variable. The property here is setting the data source for the pie chart. I would say the general effect is that you are hiding the pie chart and then exposing only a subset of the pie charts properties and methods -- anything belong to the private pie chart variable that is not exposed as a class method or property becomes essential inaccessible. It might be a little strained as there may not be any particular reason to hide a pie chart this way but it exemplifies the concept of encapsulation which is one of the hallmarks of object-oriented programming using classes.

Thanks for your explanation.

I personally would've written this:

Code:
Option Explicit

Private pDataRange As Range
Private pDataPointToExplode As Integer
Private pTitle As String

Private PieChart As Chart           'the chart we're creating
Private ThisSheet As Worksheet 'the worksheet we're on

Public Property Let DataRange(ThisRange As String)

    pDataRange = ThisRange

PieChart.SetSourceData ThisSheet.Range(pDataRange), PlotBy:=xlRows

End Property

Public Property Let DataPointToExplode(ThisPoint As Integer)

    pDataPointToExplode = ThisPoint

'explode nth point on first and only data seriesr
PieChart.SeriesCollection(1).Points(pDataPointToExplode).Explosion = 15

End Property

Public Property Let Title(ThisTitle As String)

    pTitle = ThisTitle

'the chart has a title - set its text
PieChart.HasTitle = True
With PieChart.ChartTitle
.Characters.Text = pTitle
.Border.Weight = xlHairline
.Shadow = True
End With

End Property

Private Sub Class_Initialize()

    'store which worksheet we are on
Set ThisSheet = ActiveSheet

'create the new chart, and set some basic things about it
Set PieChart = Charts.Add

'what sort of chart this is
PieChart.ChartType = xlPie

'where it is (separate sheet)
PieChart.Location Where:=xlLocationAsNewSheet

End Sub

Sub PrintPreview()

    PieChart.PrintPreview

End Sub
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,435
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Why? What purpose do those added variables actually serve?
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,404

ADVERTISEMENT

Why? What purpose do those added variables actually serve?

If you're referring to these three:

Private pDataRange As Ran
Private pDataPointToExplode As Integ
Private pTitle As String

it enables you to set the variables.

Admittedly it still works without them, as per the original but I thought adding them in makes the syntax "consistent" with the way classes are usually introduced.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,951
The second code in the OP looks like its a class for a PieChart.

The value of a class's properties has to be stored somewhere. In that class those properties are stored in the PieChart itself rather than as a variable in a module.

The class's Title property is one example of it. The title string of a PieChart is being stored in the Chart rather than in a variable.
My question about the code in the OP class, is "why no property Get?"
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,404

ADVERTISEMENT

The second code in the OP looks like its a class for a PieChart.

The value of a class's properties has to be stored somewhere. In that class those properties are stored in the PieChart itself rather than as a variable in a module.

The class's Title property is one example of it. The title string of a PieChart is being stored in the Chart rather than in a variable.
My question about the code in the OP class, is "why no property Get?"

Thanks for your explanation.

Not sure why there's no Get Property.

The original code was taken from here:

Code:
https://www.wiseowl.co.uk/vba-macros/exercises/advanced/class-modules/412/
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,435
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
My point was that if the variables aren't actually used for anything else, there really isn't any point to them.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,167
Messages
5,623,123
Members
415,956
Latest member
Footballtend

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