Reputation: 698
I will do my best to word this question properly.
The VBA IDE has this feature, where it will change the case of the characters in variable names to the case in which you type the variable name last.
For Instance, let us say I started off with the following line in a SUB:
VAR1 = 1
Now, let us say I type in the following line:
var1 = 2
The entire sub now becomes:
var1 = 1
var1 = 2
I know I can stop this from happening by declaring the variables using the Dim statement, or any of the scoping statements.
Problem: I am using a Enum in a class module. And one of the class's Public Properties is associated with said Enum. I have capitalized certain characters in the member names to make it readable (camel case).
Now, when the user is setting that property in their standard code module, if they type it all in lowercase, the Enum definition in the class module changes along with it.
Is there a way to stop that from happening?
Upvotes: 0
Views: 1878
Reputation: 31
If you setup an enum like so:
Public Enum AccountCols
AccountNo = 1
ItemDate
Code
Amount
End Enum
You can refer to it while coding by the name of the enum, followed by a dot, at which point you can select the correct name. This way, you don't have to worry about the case changing, plus you get reminded of what values are in the enum. Using the example enum above, you might type:
Set rngAmount = Cells(lngRow, AccountCols.
at which point you would see the list of names and be able to choose the correct one. No worries about the case changing then. (And a bonus is that the name of the enum itself will change to the case given in the definition, rather than staying as written in the sub.)
Upvotes: 0
Reputation: 61995
Option Explicit
and Dim
protects variable names. But it will not protect value names in enumerations.
Example:
Option Explicit
Private Enum MyEnum
Item1 = 1
iTem2 = 2
CamelCaseItem = 3
End Enum
Private CamelCaseVariable As Integer
Sub test()
Dim myVariable As MyEnum
myVariable = Item1 + CamelCaseItem
'myVariable = camelcaseitem
Debug.Print myVariable
'camelcasevariable = 3
End Sub
If you uncomment the 'myVariable = camelcaseitem
then the letter case of the value name CamelCaseItem
will change within the enumeration declaration.
If you uncomment the 'camelcasevariable = 3
then the letter case in this line will change but not the case in the declaration.
Upvotes: 2
Reputation: 427
Is there a way to stop that from happening?s there a way to stop that from happening?
It seems me that the answer is NO. For many years I put up it and simply keep in mind this behavior. Moreover it's right for standard modules too. .
Upvotes: 2