Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Public variable and Public Property in VB.NET? (Code Analysis VS2010, CA1051 : Microsoft.Design)

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?

like image 741
afuzzyllama Avatar asked Nov 27 '25 19:11

afuzzyllama


1 Answers

Using 'Property' allows you to define public access methods for your variables instead of making those variables public. See here for a good explanation: http://msdn.microsoft.com/en-us/library/65zdfbdt%28v=vs.71%29.aspx

like image 123
boto Avatar answered Nov 29 '25 11:11

boto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!