Best Practice re: get class property?

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
I have a bit of code that I am using in the GET statement of a custom class property.

In this code, I am using the values of other properties to set the values of this property. To get the other property values I used me.property.

In stepping through the code, I notice that vba goes through the whole GET statement for that property every time it comes across a me.property statment.

This seems to me like its wasting time an resources which I could save by defining a variable and setting it to the property value I need. I could use that variable in place of the me.property statements, and it wouldn't have to look up that property multiple times.

My question is about wether it would be considered good practice to do it that way (with a variable) or if its better to use me.property.

I've only been using VBA for a couple weeks, and the only other programming experience I have is some basic stuff in flash. I want to form good habits as I am learning, so I would like to know what would be considered best practice here so I can make a habit of doing it right.

Here is the code I'm working on:
Code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Get Description() As String
    '''''''''''''''''''''''''
    Dim dblMetric                   As Double
    Dim dblLow                      As Double
    Dim dblHigh                     As Double
    Dim strLow                      As String
    Dim strHigh                     As String
    Dim strNominal                  As String
    Dim strHiTol                    As String
    Dim strLoTol                    As String
    Dim strFormat                   As String
    Dim strTolerances               As String
    Dim strDimensions               As String
    '''''''''''''''''''''''''
    ''Set input unit divisor based on metric or standard:
    If Me.Metric = True Then dblMetric = 25.4 Else dblMetric = 1
    If dblMetric = 25.4 Then strFormat = ".0000" Else strFormat = ".000"
    '''''''''''''''''''''''''
    strHiTol = Format(Me.HighTolerance, Me.FormatHigh) 'set decimal places to match input
    strLoTol = Format(Me.LowTolerance, Me.FormatLow) 'set decimal places to match input
    '''''''''''''''''''''''''
    ''Define nominal, low, and high string values:
    dblLow = Me.Nominal - Me.LowTolerance
    dblHigh = Me.Nominal + Me.HighTolerance
    strNominal = Format(Me.Nominal / dblMetric, [strFormat])
    If dblLow = 0 Then strLow = "" Else strLow = Format(dblLow / dblMetric, [strFormat])
    If dblHigh = 0 Then strHigh = "" Else strHigh = Format(dblHigh / dblMetric, [strFormat])
    '''''''''''''''''''''''''
    ''Create tolerances string:
    If Me.HighTolerance = 0 And Me.LowTolerance = 0 Then 'if no tolerances, empty string
            strTolerances = ""
        ElseIf Me.HighTolerance <> Me.LowTolerance Then 'if tolerances are not equal, +tol/-tol
            strTolerances = "+" & strHiTol & "/-" & strLoTol
        ElseIf Me.HighTolerance = Me.LowTolerance Then 'if tolerances are equal, `tolerance
            strTolerances = "`" & strHiTol
        End If
    '''''''''''''''''''''''''
    ''Create (low/nom/high) string
    If Me.Nominal = 0 Then 'if no nominal value, no dimensions
            strDimensions = ""
        ElseIf Me.Metric = False And Me.LowTolerance = 0 And Me.HighTolerance = 0 Then 'if no tols, and not metric, empty string
            strDimensions = ""
        ElseIf Me.Metric = True And Me.LowTolerance = 0 And Me.HighTolerance = 0 Then 'if no tols, and is metric, (nominal)
            strDimensions = "(" & strNominal & ")"
        ElseIf Me.LowTolerance = 0 And Me.HighTolerance <> 0 Then 'if only high tol, (nom/high)
            strDimensions = "(" & strNominal & " / " & strHigh & ")"
        ElseIf Me.LowTolerance <> 0 And Me.HighTolerance = 0 Then 'if only low tol, (low/nom)
            strDimensions = "(" & strLow & " / " & strNominal & ")"
        ElseIf Me.LowTolerance <> 0 And Me.HighTolerance <> 0 Then 'if both tols, (low/nom/high)
            strDimensions = "(" & strLow & " / " & strNominal & " / " & strHigh & ")"
        End If
    '''''''''''''''''''''''''
    ''Set Description property:
    Description = Me.InputDescription & " " & strTolerances & " " & strDimensions
End Property
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
My approach is not to worry about optimizing (or micro-optimizing) unless I expect (or find) real problems with performance. Such a call to your property procedures may take only microseconds at runtime.

Using the properties has the advantages of intellisense when writing the code, the simplicity of the property names, and the encapsulation of *all* set/get activity in the property procedure and nowhere else - this means your class variables are well controlled. It seems quite odd when you are stepping through the code and jumping around a lot but you'll never notice it at runtime.

In reality - I've done both, but I think I would prefer sticking to the property methods in a large project for the reasons stated above.
 
Upvote 0
Xenou, the reason I actually used the me.property method was because of intellisense ;)

You raise some good points about run-time performance vs stepping through manually. It does seem odd when stepping through manually though. Thank you for the advice and insight!
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,903
Members
452,948
Latest member
Dupuhini

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