Gainster
Gainster

Reputation: 5631

Naming convention for windows control in C#

I want to know what the guidance on name the control in c#. I have seen the following link but it does not talks about controls: http://msdn.microsoft.com/en-us/library/ms229002.aspx

Other links tell me to use hungarian notation but some links tell me it is bad practice to use hungarian notation.

Personally using hangarian notation seems ok to me for controls. http://vb.byu.edu/vb/HungarianNotation.htm

But I am not sure, need some guidance ?

Upvotes: 0

Views: 965

Answers (4)

Jeffrey L Whitledge
Jeffrey L Whitledge

Reputation: 59553

While I am personally opposed to Hungarian notation, I tend to do something similar with the names of Windows controls. Rather than use a prefix, however, I tend to put the type of control as a suffix.

liquidHydrogenCapacityTextBox
extraKetchupCheckBox
fileOpenMenuItem
favoriteFormerNSyncMemberDropDownList

I think this is OK, not because it describes the type of the variable, but because it is descriptive of what the variable actually contains.

Upvotes: 2

Joe White
Joe White

Reputation: 97848

I'll take the minority position here, and say that our shop still generally uses Hungarian notation for WinForms controls. Mostly because that was the standard back when we first wrote our UI, and there's been no reason to go back and do mass renames.

As we write new UI code though, especially WPF, we've been drifting more toward straightforward names like "userNameTextBox" (on those rare occasions when we need to name WPF controls at all). It's a bit more readable.

Upvotes: 0

LazyOfT
LazyOfT

Reputation: 1438

I used Hungarian notation for a while, but the main problem is that if you change the underlying type of your control you're kind of stuck into the name you decided. Nowadays refactoring tools are helping, but in my everyday job I keep coming across "calSomething" or "edtWhatever" which are not calendars and editors anymore. I remember that Microsoft was used to that too, so we still have wParam and lParam in the Windows SDK, despite the fact that they are basically the same since the advent of 32bit operating systems.

My suggestion is to not use Hungarian notation at all, but choosing meaningful names for your controls. Intellisense and such are more than helpful for determining the type of your control the majority of time, and using a prefix sometimes could make your code less clear to the occasional reader, because it diverts your attention from the semantics of your program to the mechanisms used by it.

Upvotes: 1

Sam Axe
Sam Axe

Reputation: 33738

name it using the same convertion you use for the rest of your code.

if your code uses Hungarian, then name it using that.. if your code uses something else, use that.

The one exception would be public-facing API (types, enums, etc).. I would suggest you take the route MS did and name things camel-case without prefixes and other cruft.

Upvotes: 2

Related Questions