When running code analysis on my project, I receive the following message:
CA1051 : Microsoft.Design : Because field 'ClassName.VarName' is visible outside of its declaring type, change its accessibility to private and add a property, with the same accessibility as the field has currently, to provide access to it.
To resolve this, I can change the following line:
Public VarName As String
to this:
Public Property VarName As String
I don’t understand why the Property keyword is so important in this particular case. Can anyone provide an explanation as to why changing this member to a Property makes a significant difference to code analysis? Am I doing something wrong?
Public Property VarName As Stringdeclares a property providing a level of encapsulation. You will be able to access the private member state of this variable within your class by using _VarName.Public VarName As Stringdeclares a variable that will just provide a single public instance of this variable and no encapsulation.