Search code examples
vb.netnamingnaming-conventions

How do you name member variables in VB.NET?


I am generally not one to engage in subjective arguments over matters like variable naming, code formatting, etc. So I have no intention of starting an argument here.

I just came across this (old) blog post which recommends not prefixing member variable names:

Do not use a prefix for member variables (_, m_, s_, etc.). If you want to distinguish between local and member variables you should use "this." in C# and "Me." in VB.NET.

For C#, yeah, I get it: member variables can be lower camelCase, and public properties/methods can be PascalCase. But VB.NET is case-insensitive, so you can't really give a private member the same name as a public property except with a lower case first letter.

I've generally prefixed member variables with an underscore, but I've been told that's not idiomatic.

So really I'm just curious: how do you name your member variables in VB.NET? And is there a "standard" way?

I'm not asking because I believe there's a "right" way or because I particularly want to change my style, and certainly not because I have any desire to tell others they're "wrong." Like I said, I'm just curious.


Solution

  • It's personal preference, although there's widespread support for having some distinction. Even in C# I don't think there's one widely used convention.

    Jeff Prosise says

    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.

    From the .NET Framework Design Guidelines 2nd Edition page 73.

    Jeffrey Richter says

    I make all my fields private and I prefix my instance fields with "m_" and my static fields with "s_" [in C#]

    From the .NET Framework Design Guidelines 2nd Edition page 47. Anthony Moore (BCL team) also thinks using "m_" and "s_" is worth consideration, page 48.