Pierre Greborio.NET

Talking about .NET world

Naming Guidelines and Visual Controls

According to the .NET naming guidelines it is often suggested to adopt camel case for private properties/fields and pascal case for protected and public properties/fields.

It is unclear how to name visual controls. Let consider a standard label which is protected in a web form. We could name as following:

  1. LblFirstName (classic pascal case for protected fields)
  2. StaticFirstName (contro, agnostic version)
  3. lblFirstName (as usually I do)
  4. _lblFirstName (C++ style)

For read/write controls we could use:

  1. TxtFirstName
  2. ReadWriteFirstName
  3. txtFirstName
  4. _txtFirstName

During these last 10 years of development I changed my programming style. I started with 4, then moved to 3. Today I'm thinking about the 2.

Comments

Phil Scott said:

I'd go with FirstNameLabel myself, and have since 2002. I thought this was the guidelines they were pushing for visual controls.

In fact, try creating a menu with the MenuStrip control in VS2005. If you create a menu item with the caption of Test VS2005 will automatically name it TestToolStripMenuItem.
# December 19, 2005 11:58 AM

Jeff Gonzalez said:

I prefer the more Verbose style of naming:

FirstNameTextBox
CustomerIdDropDownList

After all, we have intellisense =)
# December 19, 2005 12:06 PM

Robert McLaws said:

I use TextBox_FirstName for my controls. Keeps my code more readable, since we don't have to be SO concerned about the memory footprint. It also helps with Intellisense when you're coding.

It doesn't play as nice as you'd like with the automatic event handler naming conventions, but it still works.
# December 19, 2005 1:15 PM

Kent Sharkey said:

I agree with Phil & Jeff to use a "Property-like" name for the controls - although I tend to use FirstNameCaption (Label) and FirstNameField (TextBox) for my visible controls. Member variables (that back properties) are _whatever and parameters are camelCased as per the guidelines.
# December 19, 2005 2:24 PM

lexp said:

I personally use notation like

MyFirstNameTextBox

"My" prefix is used mainly for intellisense.
# December 20, 2005 10:51 AM