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:
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
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''