Naming Conventions

T

Ted

I'm trying to come up with naming conventions for my
company as we move to C#. I'm looking through the Naming
Guidelines section on MSDN, but I'm unable to find a
recommendation for class scope variables.

Working with WinForms, I believe it would be helpful to
have some type of prefix. In Visual Basic we used
m_variableName. But I can't find any suggestions on this.

Any help on this would be great. Thanks,
Ted
 
R

Rob Windsor [MVP]

Hi Ted

Microsoft suggests the following:

Public methods or properties are pascal cased. (mixed case - first letter
upper case)
Private or protected methods are camel cased. (mixed case - first letter
lower case)
Private or protected fields (variables) are camel cased and prefixed with an
underscore.
Variable names do not use Hungarian notation.
Parameters are camel cased.

Public Class TestClass
Private _stringField1 As String
Protected _stringField2 As String

' public fields are not recommended
' use properties instead
Public StringField3 As String

Public Property StringField1() As String
Get
Return _stringField1
End Get
Set(ByVal Value As String)
_stringField1 = Value
End Set
End Property

Public Sub CoolRoutine1(ByVal param1 As String)
End Sub

Private Sub coolRoutine2()
End Sub
End Class

There's a MSDN Webcast on .NET Best Pratices that discusses this over a few
slides.
 
C

Cor

Hi Rob,

I have checked for it in the VB.resourcekit and there is this what you show
absolute not used in the best practise sample, so you make me curious.

Also because the type of a object is very good to recognise in dotnet it is
advice to add that not in your dataname, so I am very curious.

Can you give me the link to that documentation.

Cor
 
T

Ted

Rob,

Can you send me a link where this is suggested?

In the Design Guidelines for Class Library Developers
microsoft is very inconsistent with naming conventions
while suggesting proper naming conventions.

For example, at http://msdn.microsoft.com/library/en-
us/cpgenref/html/cpconpropertyusageguidelines.asp?
frame=true

Microsoft lists the example:

[Visual Basic]
Public Class TextBox
Private m_dataSource As String
Private m_dataField As String
Private m_active As Boolean

Public Property DataSource() As String
Get
Return m_dataSource
End Get
Set (...more)

What is the "m_" prefix? If it's for Module, then module
has a different meaning in .net.

The C# example:

public class TextBox
{
string dataSource;
string dataField;
bool active;

public string DataSource
{
get
{
return dataSource;
}
set (...more)

Now for C# they do not use the prefix.

But what if the constructor accepted the dataSource
variable in the C# example, what would the variable name
for the constructor be? ie. localDataSource or
thisDataSource?

Rob, I like your example of prefixing with the underscore
and will probably go with this, but I just can not find
that recommendation on Microsoft's website in the
guidelines section.

Thank you,
Ted


-----Original Message-----
Hi Ted

Microsoft suggests the following:

Public methods or properties are pascal cased. (mixed case - first letter
upper case)
Private or protected methods are camel cased. (mixed case - first letter
lower case)
Private or protected fields (variables) are camel cased and prefixed with an
underscore.
Variable names do not use Hungarian notation.
Parameters are camel cased.

Public Class TestClass
Private _stringField1 As String
Protected _stringField2 As String

' public fields are not recommended
' use properties instead
Public StringField3 As String

Public Property StringField1() As String
Get
Return _stringField1
End Get
Set(ByVal Value As String)
_stringField1 = Value
End Set
End Property

Public Sub CoolRoutine1(ByVal param1 As String)
End Sub

Private Sub coolRoutine2()
End Sub
End Class

There's a MSDN Webcast on .NET Best Pratices that discusses this over a few
slides.

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada



I'm trying to come up with naming conventions for my
company as we move to C#. I'm looking through the Naming
Guidelines section on MSDN, but I'm unable to find a
recommendation for class scope variables.

Working with WinForms, I believe it would be helpful to
have some type of prefix. In Visual Basic we used
m_variableName. But I can't find any suggestions on this.

Any help on this would be great. Thanks,
Ted


.
 
G

Guest

Hi,
The convention suggested by Rob has been defined in the book "Inside C#" by Tom Archer, Microsoft Press.

The link you mentioned seems to be going along with the conventions listed in the book, and by Rob. Only exception being the private variables, which are prefixed by 'm_'.

So clases have names like: ClassName
Methods: MethodName
Properties: PropertyName
Private Attributes: m_attributeName
Other Attributes: attributeName

Regards,
Jaswinder
 
C

Cor

Hi Jaswinder,

I follow this thread also, and it is in my opinion not true what you write.

Rob is giving to stringvalues very clear the name
stringName

In my opinion Name is enough

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top