<p>Is there an official convention for naming private fields in VB.NET?  For example, if I have a property called 'Foo', I normally call the private field '_Foo'.  This seems to be frowned upon in the <a href="http://msdn.microsoft.com/en-us/library/ms229012.aspx" rel="noreferrer">Offical Guidelines</a>:</p>

<p>"Do not use a prefix for field names. For example, do not use g_ or s_ to distinguish static versus non-static fields."</p>

<p>In C#, you could call the private field 'foo', the property 'Foo', and refer to the private field as 'this.foo' in the constructor.  As VB.NET is case insensitive you can't do this - any suggestions?</p>

[{'answer_id': 7644, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 5, 'selected': True, 'text': '<p>I still use the _ prefix in VB for private fields, so I\'ll have _foo as the private field and Foo as the property. I do this for c# as well and pretty much any code I write. Generally I wouldn\'t get too caught up in "what is the right way to do it" because there isn\'t really a "right" way (altho there are some very bad ways) but rather be concerned with doing it consistently.</p>\n\n<p>At the end of the day, being consistent will make your code much more readable and maintainable than using any set of "right" conventions.</p>\n'}, {'answer_id': 7646, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': '<p>Official guidelines are just that -- guidelines. You can always go around them. That being said we usually prefix fields with an underscore in <em>both</em> C# and VB.NET. This convention is quite common (and obviously, the Official Guidelines ignored).</p>\n\n<p>Private fields can then be referenced without the "me" keyword (the "this" keyword is for C# :)</p>\n'}, {'answer_id': 7647, 'author': 'BTB', 'author_id': 961, 'author_profile': 'https://Stackoverflow.com/users/961', 'pm_score': 2, 'selected': False, 'text': "<p>I don't think there is an official naming convention, but i've seen that Microsoft use m_ in the Microsoft.VisualBasic dll (via reflector).</p>\n"}, {'answer_id': 7753, 'author': 'urini', 'author_id': 373, 'author_profile': 'https://Stackoverflow.com/users/373', 'pm_score': 0, 'selected': False, 'text': '<p>I agree with @lomaxx, it\'s more important to be consistent throughout the team than to have the <em>right</em> convention.</p>\n\n<p>Still, here are several good places to get ideas and guidance for coding conventions:</p>\n\n<ol>\n<li><a href="http://www.dotnet2themax.com/PracticalGuidelines.aspx" rel="nofollow noreferrer">Practical Guidelines and Best Practices for Microsoft Visual Basic and Visual C#</a> Developers by Francesco Balena is a great book that addresses many of these issues.</li>\n<li><a href="http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=0&amp;tabid=1" rel="nofollow noreferrer">IDesign Coding Standards</a> (for C# and for WCF)</li>\n<li>The .NET Framework <a href="http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx" rel="nofollow noreferrer">Source Code</a> (in VS2008)</li>\n</ol>\n'}, {'answer_id': 8092, 'author': 'Lance Fisher', 'author_id': 571, 'author_profile': 'https://Stackoverflow.com/users/571', 'pm_score': 0, 'selected': False, 'text': "<p>I prefer to use the underscore prefix for private fields.  I use lowercase first letter for the method parameters.  I follow the guideline of having lowercase camelcase parameters for methods, which I regard as more important than the naming of private fields since it is part of the API for the class.  . e.g.</p>\n\n<pre><code>Public Class Class1\n\n    Private _foo As String\n    Public Property Foo() As String\n        Get\n            Return _foo\n        End Get\n        Set(ByVal value As String)\n            _foo = value\n        End Set\n    End Property\n\n    Public Sub New(ByVal foo As String)\n        _foo = foo\n    End Sub\n\nEnd Class\n</code></pre>\n\n<p>Using this pattern, you won't have any naming conflicts with the private field and your constructor parameter in C# or VB.NET.</p>\n"}, {'answer_id': 9001, 'author': 'Brad Tutterow', 'author_id': 308, 'author_profile': 'https://Stackoverflow.com/users/308', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>I still use the _ prefix in VB for\n  private fields, so I\'ll have _foo as\n  the private field and Foo as the\n  property. I do this for c# as well and\n  pretty much any code I write.\n  Generally I wouldn\'t get too caught up\n  in "what is the right way to do it"\n  because there isn\'t really a "right"\n  way (altho there are some very bad\n  ways) but rather be concerned with\n  doing it consistently.</p>\n</blockquote>\n\n<p>I haven\'t found anything better than the "_" for clarify and consistency.  Cons include:</p>\n\n<ul>\n<li><a href="http://msdn.microsoft.com/en-us/library/y9a1wfdd(VS.71).aspx" rel="nofollow noreferrer">Not CLS compliant</a></li>\n<li>Tends to get lost when VB draws horizontal lines across my IDE</li>\n</ul>\n\n<p>I get around the lines by turning those off in the editor, and try not to think too much about the CLS compliance.</p>\n'}, {'answer_id': 33031, 'author': 'OwenP', 'author_id': 2547, 'author_profile': 'https://Stackoverflow.com/users/2547', 'pm_score': 2, 'selected': False, 'text': '<p>The design guidelines that you linked specifically state that they only apply to static public and protected fields.  The design guidelines mostly focus on designing public APIs; what you do with your private members is up to you.  I\'m not positive but I\'m relatively confident that private members are not considered when the compiler checks for CLS compliance, because only public/protected members come in to play there (the idea is, "What if someone who uses a language that doesn\'t allow the _ character tries to use your library?"  If the members are private, the answer is  "Nothing, the user doesn\'t have to use these members." but if the members are public you\'re in trouble.)</p>\n\n<p>That said, I\'m going to add to the echo chamber and point out that whatever you do, it\'s important to be consistent.  My employer mandates that private fields in both C# and VB are prefixed with _, and because all of us follow this convention it is easy to use code written by someone else.  </p>\n'}, {'answer_id': 46452, 'author': 'sergeb', 'author_id': 4808, 'author_profile': 'https://Stackoverflow.com/users/4808', 'pm_score': 0, 'selected': False, 'text': '<p>I agree most important is not what style one uses but it being consistent. </p>\n\n<p>With that said, the new MS/.NET styling for private fields tends to be _fooVar (underscore followed by a camelCased name)</p>\n'}, {'answer_id': 3014167, 'author': 'MarkJ', 'author_id': 15639, 'author_profile': 'https://Stackoverflow.com/users/15639', 'pm_score': 3, 'selected': False, 'text': '<p>It\'s personal preference, although there\'s widespread support for having <em>some</em> distinction. Even in C# I don\'t think there\'s one widely used convention.</p>\n<p><a href="https://web.archive.org/web/20110727211715/http://wintellect.com/TechnicalBioDetail.aspx?Tech=5" rel="nofollow noreferrer">Jeff Prosise</a> says</p>\n<blockquote>\n<p>As a matter of personal preference I typically prefix private fields with an underscore [in C#] ... This convention is used quite a lot in the .NET framework but it is not used throughout.</p>\n</blockquote>\n<p>From the .<a href="https://rads.stackoverflow.com/amzn/click/com/0321545613" rel="nofollow noreferrer" rel="nofollow noreferrer">NET Framework Design Guidelines</a> 2nd Edition page 73.</p>\n<p><a href="https://web.archive.org/web/20130315155154/http://www.wintellect.com/cs/blogs/jeffreyr/default.aspx" rel="nofollow noreferrer">Jeffrey Richter</a> says</p>\n<blockquote>\n<p>I make all my fields private and I prefix my instance fields with &quot;m_&quot; and my static fields with &quot;s_&quot; [in C#]</p>\n</blockquote>\n<p>From the .<a href="https://rads.stackoverflow.com/amzn/click/com/0321545613" rel="nofollow noreferrer" rel="nofollow noreferrer">NET Framework Design Guidelines</a> 2nd Edition page 47. Anthony Moore (<a href="https://learn.microsoft.com/en-us/archive/blogs/bclteam/a-brief-history-of-datetime-follow-up-anthony-moore" rel="nofollow noreferrer">BCL team</a>) also thinks using &quot;m_&quot; and &quot;s_&quot; is worth consideration, page 48.</p>\n'}, {'answer_id': 7235878, 'author': 'crunchy', 'author_id': 602542, 'author_profile': 'https://Stackoverflow.com/users/602542', 'pm_score': 2, 'selected': False, 'text': "<p>In VB.NET 4.0, most of you probably know you don't need to explicitly write getters and setters for your Property declarations as follows:</p>\n\n<pre><code>Public Property Foo As String\nPublic Property Foo2 As String\n</code></pre>\n\n<p>VB automatically creates private member variables called _Foo and _Foo2.  It seems as though Microsoft and the VS team have adopted the _ convention, so I don't see an issue with it.</p>\n"}]
['https://Stackoverflow.com/questions/7642', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/133/']