• 0

.NET Controls naming convention


Question

Hello, I am new to .NET programming. There is a set of naming convention in Java, for example, methodName for method name, CONTNT for constants. I want to know if there is also a set of naming conventions for .NET controls, such as textbox, labels, data grid...etc?

Thank you very much! :)

Link to comment
https://www.neowin.net/forum/topic/785788-net-controls-naming-convention/
Share on other sites

Recommended Posts

  • 0

You can name them anything you like. But having consistency is a great component.

Examples, for labels I use LBL_#CONTROL# and text boxes TXT_#CONTROL#

Just simple things like that, and if you have a tabbed or multi-window application, you can use something like LBL_TB2_#CONTROL# for a label on the second tab.

Thats how I roll anyway.

  • 0

The way I was taught in college is something like textBoxUserName (for a textbox control) but I completely ignored it. Instead I just use Hungarian notation which is a lot faster yet still easy to interpret. Hungarian notion is simply stuff like lblName for a label, txtName for a textbox, cboName for a combobox, lstName for a listbox, etc... That's a pretty common practice. As for non .NET control naming conventions there's a little guide on MSDN: http://msdn.microsoft.com/en-us/library/xz...28VS.71%29.aspx

This site: http://www.anticipatingminds.com/Content/p...wledgePack.aspx which references that MSDN page seems to suggest that you add the name of the control at the end of the field name. For example, a textbox for a person's first name would be "firstNameTextBox". To me that seems ridiculous as it is extra typing and simple 3 letter abbreviations seem to work perfectly fine.

Edited by dlegend
  • 0

We name our UI controls to be as self documenting as any other variable. The only exception is that they are prefixed with 'ux' indicating they are part of the user experience. We toyed with 'ui' but it didn't look as good depending on the rest of the control name. So if I had a textbox fir a user first name and property to hold the value, I might have something like uxFirstName.Text = myCLass.FirstName. We never use the control type in the control name anymore except in cases of lists and grids and even in those cases, the control name indicates the data, not the control such as uxStateList (combo) or uxMemberClaimsGird (datagridview). The benefit of the 'ux' prefix is that all controls are listed in inteillisense together.

  • 0

Systems Hungarian Notation has really fallen out of favor now. Microsoft officially recommends that you shouldn't use it.

  Quote
Do choose easily readable identifier names. For example, a property named HorizontalAlignment is more readable in English than AlignmentHorizontal.

Do favor readability over brevity. The property name CanScrollHorizontally is better than ScrollableX (an obscure reference to the X-axis).

Do not use underscores, hyphens, or any other nonalphanumeric characters.

Do not use Hungarian notation.

http://msdn.microsoft.com/en-us/library/ms229045.aspx

  • 0
  sbauer said:
Systems Hungarian Notation has really fallen out of favor now. Microsoft officially recommends that you shouldn't use it.

Yes, I've heard this as well but I still prefer to use it. Does anyone know why it's not recommended? I think it's simple and effective.

  • 0

I have been using a dirivative of the Hungarian for over a dozen years. Can someone explain the logic on why this format should not be used?

After all, looking at txtFirstName, strFirstName give more info and context that just FirstName. If I am reviewing code and see FirstName how am I to know, on sight, if that is a variable, a control or a function?

  • 0
  jakem1 said:
Yes, I've heard this as well but I still prefer to use it. Does anyone know why it's not recommended? I think it's simple and effective.

It's a form of commenting and comments lie. It's just redundant.

To be honest, I've never understood the love affair with Hungarian notation and .net. Why do you think it's simple and effective?

  • 0

I use the same as garethevans1986

In Delphi I used hungarian. In C# I use things like textBoxFirstName, checkBoxRememberPassword

I basically just delete the number that gets suffixed on the end of the new control and replace it with the text. It makes for some long control names, but I know exactly what something is.

Unfortunatley the design guidelines don't include control naming, so you're stuck coming up with your own.

  • 0
  jameswjrose said:
After all, looking at txtFirstName, strFirstName give more info and context that just FirstName. If I am reviewing code and see FirstName how am I to know, on sight, if that is a variable, a control or a function?

FirstName would be a horrible name for a method. It doesn't even have a verb in it.

  • 0
  sbauer said:
It's a form of commenting and comments lie. It's just redundant.

To be honest, I've never understood the love affair with Hungarian notation and .net. Why do you think it's simple and effective?

If I see a control called txtFirstName I instantly know that it's a textbox that is used to store first names. I can also use the same notation for variables (e.g. strFirstName is a string used to store first names) which makes my code self documenting and easy to read/write. That's what I like about it.

Out of interest, what do you use?

  • 0
  jakem1 said:
If I see a control called txtFirstName I instantly know that it's a textbox that is used to store first names. I can also use the same notation for variables (e.g. strFirstName is a string used to store first names) which makes my code self documenting and easy to read/write. That's what I like about it.

This is what I'm talking about. Completely redundant. strFirstName? Yeah, of course it's a string. It's a first name. What else could it be? It's not going to be a bool. It's not going to be an int.

Descriptive variable names that reveal intent will often save you from the redundancy that is Hungarian notation (and bad code commenting in general).

  Quote
Out of interest, what do you use?

Variable names that reveal intent. That's all.

  • 0
  The_Decryptor said:
Ehh?

First you say it's bad, then it's good, or am I just confused? ("strFirstName" is Hungarian notation)

I'm saying it's bad. I'm saying strFirstName is bad.

  Quote
This is what I'm talking about. Completely redundant. strFirstName? Yeah, of course it's a string. It's a first name. What else could it be? It's not going to be a bool. It's not going to be an int.
  • 0
  sbauer said:
This is what I'm talking about. Completely redundant. strFirstName? Yeah, of course it's a string. It's a first name. What else could it be? It's not going to be a bool. It's not going to be an int.

Descriptive variable names that reveal intent will often save you from the redundancy that is Hungarian notation (and bad code commenting in general).

strFirstName may be a bad example because first name is going to be a string. What about a user ID? It could be a string or an integer for example. strUserID or intUserID works perfectly and reveals usage and intent.

Why are you being so obtuse? At the end of the day this all just comes down to personal/team preference. There are no rules as long as the solution you choose is logical and meaningful. I'm happy to put up with a couple of strFirstName type names to make the most of an intUserID.

  • 0
  sbauer said:
This is what I'm talking about. Completely redundant. strFirstName? Yeah, of course it's a string. It's a first name. What else could it be? It's not going to be a bool. It's not going to be an int.

Descriptive variable names that reveal intent will often save you from the redundancy that is Hungarian notation (and bad code commenting in general).

The 'str' is not redundant, it is there to show it's a string and not a control eg edFirstName would be an edit control.

  • 0
  jakem1 said:
strFirstName may be a bad example because first name is going to be a string. What about a user ID? It could be a string or an integer for example. strUserID or intUserID works perfectly and reveals usage and intent.

1) Once I realize the standard, I would never need to carry the baggage around throughout the entire application.

User user = new User();

user.strUserID = "B192" <-- ugly.

Or 2) I would put my mouse over the variable name.

Or 3) Develop a standard where UserID = int/guid, UserNumber = string

Seriously, though, you're going to add a ton of extra baggage to your code just so you can see what type the ID is without having to think?

  Quote
Why are you being so obtuse? At the end of the day this all just comes down to personal/team preference. There are no rules as long as the solution you choose is logical and meaningful. I'm happy to put up with a couple of strFirstName type names to make the most of an intUserID.

I'm not being obtuse. The original poster asked for advice, and he got a lot of advice that I didn't agree with. Therefore, I'm going to share my opinion. My opinion isn't completely different. Microsoft, Robert Martin (author of Clean Code), and Linus Torvalds all share the same opinion when in comes to Systems Hungarian notation.

I realize it's a team preference. My team used sourcesafe before I came. That, like this, was a team preference, but it certainly wasn't the best option.

  • 0
  Mike said:
The 'str' is not redundant, it is there to show it's a string and not a control eg edFirstName would be an edit control.

Why do you even use IDEs? Use App Hungarian notation for the ui components, but don't pollute other stuff. Or what purpose is the strFirstName serving as a private field within the page or ui form?

  • 0
  sbauer said:
Why do you even use IDEs? Use App Hungarian notation for the ui components, but don't pollute other stuff. Or what purpose is the strFirstName serving as a private field within the page or ui form?

I use IDEs to make the building process easier and so I don't need to manually add new files to be built etc.

The whole 'hover over a variable to see what type it is' is a bad idea. Why? Because it means you (personally) don't know what the variable type is.

The strFirstName variable could be the result of a simple edFirstName->GetText() call and the resultant string could be used to show a message or whatever. If you just had FirstName for the edit control, what would the variable be called that took the string?

  • 0
  Mike said:
The whole 'hover over a variable to see what type it is' is a bad idea. Why? Because it means you (personally) don't know what the variable type is.

That doesn't make any sense. You don't know the type either. You're just guessing the type is the same as the prefix. The prefix is a comment. That's all it is. If someone changed the type on you without refactoring the variable name, you'd be wrong.

  Mike said:
The strFirstName variable could be the result of a simple edFirstName->GetText() call and the resultant string could be used to show a message or whatever. If you just had FirstName for the edit control, what would the variable be called that took the string?

Fine. Break out the Apps Hungarian notation, and prefix all the controls with a ui prefix. string firstName = uiFirstName.Text. That's an OK standard. I would never pollute my code with the typical Systems Hungarian notation crap, though.

Edited by sbauer
  • 0
  sbauer said:
That doesn't make any sense. You don't know the type either. You're just guessing the type is the same as the prefix. The prefix is a comment. That's all it is. If someone changed the type on you without refactoring the variable name, you'd be wrong.

No i'm not guessing, the prefixes are chosen for a reason. They aren't just some random letters. If someone did change the type and didn't change the prefix they would end up with a sore backside.

  sbauer said:
Fine. Break out the Apps Hungarian notation, and prefix all the controls with a ui prefix. string firstName = uiFirstName.Text. I would never pollute my code with the typical Systems Hungarian notation crap.

The 'ui' prefix doesn't help with distinguishing the type. It's a similar case with prefixing global variables so you know that if you change it some place, it won't just be your code that is affected.

  • 0
  Mike said:
No i'm not guessing, the prefixes are chosen for a reason. They aren't just some random letters. If someone did change the type and didn't change the prefix they would end up with a sore backside.

I know they're not some random letters. I get the concept. I just don't believe in it. I think the investment is not worth what you get back.

  Quote
The 'ui' prefix doesn't help with distinguishing the type. It's a similar case with prefixing global variables so you know that if you change it some place, it won't just be your code that is affected.

Yeah, I don't care about that either. I don't want type information in my variable names. It's unnecessary and wasn't what Hungarian notation was designed to be.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Hahaha. You'll be surprised at the rigor they put in coming with these decisions.
    • There is but look up training videos to do data analysis in Excel v the app they use in libre office? Don't even know the name. Besides we are talking about govt employees. Have you tried explaining command line to a govt employee? Look I respect the people but I know that they could not be bothered with open source
    • Let's see how long this lasts. In the end, it comes down to productivity lost because of workflow disruptions. It's not even a question of "which is better", rather how painful will it be to switch and it's hard enough for a single person to switch - imagine an entire city's bureaucracy. Remember, there are governmental system in the US that are still using 5.25" floppy disks... Having been involved in these kinds of swaps, I can tell you - it's never as easy as the fanbase thinks it is.
    • Right, saw it in the microsoft blog, wasn't mentioned in the article, thanks.
    • Multiple internal and external HDDs from Seagate, Western Digital are now at great prices by Fiza Ali Amazon and Newegg are currently offering substantial discounts on a wide selection of internal and external hard drives from Seagate and Western Digital, with prices reduced across multiple capacities. The 4TB WD Purple Surveillance is a 3.5-inch SATA III drive offering sustained transfer rates of up to 175MB/s. It employs Conventional Magnetic Recording (CMR) with a 256MB cache buffer. The drive operates reliably between 0°C and 65°C and can be stored in temperatures ranging from –40°C to 70°C. Western Digital backs this unit with a three-year limited warranty as well. 4TB WD Purple Surveillance Internal HDD: $84.41 (Amazon US) - 8% off The 6TB WD Blue is also a 3.5-inch internal hard drive that connects via SATA III (6Gb/s) and delivers sustained transfer rates of up to 185MB/s. It spins at 5,400 RPM, employs Conventional Magnetic Recording (CMR) technology, and features a 256MB cache buffer. The drive operates reliably in temperatures from 0°C to 60°C (with safe storage down to –40°C and up to 70°C). It is backed by a two-year limited manufacturer’s warranty. 6TB WD Blue PC Internal HDD: $99.99 (Amazon US) - 17% off The 10TB WD Red Pro NAS drive comes in a 3.5-inch form factor and connects via SATA III (6Gb/s). It sustains transfer speeds of up to 267MB/s thanks to its 7,200 RPM spindle and 512MB cache buffer, and employs Conventional Magnetic Recording (CMR) for reliable multi-drive operation. It operates safely between 0°C and 65°C, can be stored or transported in temperatures from –40°C to 70°C, and is covered by Western Digital’s five-year limited warranty. 10TB WD Red Pro NAS Internal HDD: $237.49 (Amazon US) - 15% off This WD Elements Desktop external hard drive offers a 14TB of storage via a USB 3.0 interface (up to 5Gb/s), using a USB Micro-B connector that is backward-compatible with USB 2.0. It operates reliably between 5°C and 35°C and can be stored in temperatures ranging from –20°C to 65°C. The drive is powered by an external adapter and carries a two-year limited warranty. 14TB WD Elements Desktop External HDD: $199.99 (Amazon US) - 31% off The 16TB Seagate Expansion Desktop external hard drive delivers vast storage capacity in a simple, plug-and-play design. USB 3.0 connectivity provides high-speed data transfer rates. Out of the box, the Expansion Desktop model is recognised automatically by Windows, macOS, and ChromeOS systems. If you wish to use Apple’s Time Machine backup utility, the drive must be reformatted to the HFS+ file system. 16TB Seagate Expansion Desktop External HDD: $229.99 (Newegg) - 30% off The 16TB WD Elements desktop external HDD connects via a USB 3.0 interface using a Micro-B cable (up to 5Gb/s.) The drive features plug-and-play functionality, working straight out of the box with Windows PCs. It operates reliably in ambient temperatures from 5°C to 35°C and can be stored in temperatures ranging from –20°C to 65°C. The drive comes with a 2-year limited warranty as well. 16TB WD Elements Desktop External HDD: $249.99 + $20 off promo code SAAET2384 = 229.99 (Newegg) The 16TB Seagate BarraCuda 3.5-inch internal HDD offers Multi-Tier Caching Technology (MTC) which balances NAND flash, DRAM, and media cache layers to accelerate application launches, reduce load times, and maintain consistently high sustained read/write speeds. The included Seagate DiscWizard software simplifies drive migration, cloning, partitioning, and backup tasks. The drive is covered by a two-year limited warranty. 16TB Seagate BarraCuda Internal HDD: $194.99 (Newegg) - 7% off The 20TB Seagate Exos X20 delivers an enterprise-class solution for high-density storage environments and data centres. It offers a sustained sequential transfer rate of up to 285MB/s and advanced caching to ensure low-latency, repeatable response times for data-intensive workloads. It further features 550TB/year workload rating, 2.5 million-hour mean time between failures (MTBF), and five-year limited warranty. PowerChoice and PowerBalance technologies allow administrators to tailor power consumption profiles for active and idle states, reducing energy costs and cooling requirements. Hardware-based AES-256 encryption, password protection, and Seagate Secure certification safeguard sensitive data. 20TB Seagate Exos X20 Internal HDD: $379 + $50 off promo code EPET2523 = $329.99 (Newegg) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • Collaborator
      Mighty Pen went up a rank
      Collaborator
    • Week One Done
      emptyother earned a badge
      Week One Done
    • Week One Done
      DarkWun earned a badge
      Week One Done
    • Very Popular
      valkyr09 earned a badge
      Very Popular
    • Week One Done
      suprememobiles earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      569
    2. 2
      +FloatingFatMan
      180
    3. 3
      ATLien_0
      175
    4. 4
      Xenon
      116
    5. 5
      Som
      110
  • Tell a friend

    Love Neowin? Tell a friend!