• 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

    • Here are the top 5 features people still want in Microsoft Teams by Usama Jawad Microsoft Teams is one of the most popular online communication and collaboration tool out there, especially in enterprise environments. Although it receives new features fairly regularly, customers are always clamoring for more, which makes sense given the fact that there are lots of usage scenarios that require coverage when you have millions of users. Back in 2022, we listed the top 10 features that people wanted in Microsoft Teams, and the Redmond tech did roll out some of them globally in the past years. Examples of those include the ability to use multi-tenant accounts and delete private messages. Now, it's time to revisit the same topic in a similar fashion with a list of the top five requests. It is important to note that these feature requests highlighted below are based on the dedicated Feedback portal, are ranked by upvotes, and only include those which have not yet been fulfilled by Microsoft: Enable us to move channels from one team to another (39,451 upvotes): This is similar to what was being requested back in 2022, but Microsoft is still ideating on how to implement this functionality. The item is still in the backlog and the scope and design of the solution are yet to be figured out, and the company is asking for further feedback and ideas in this area. Include Office 365 group calendar (26,998 upvotes): Again a request carrying on from our previous coverage. At that time, Microsoft said that this feature is a priority for it and users will see incremental updates soon. However, the enhancement has now been deprioritized and has been entered in the consideration phase again. The effort to implement this solution would require cross-functional collaboration which doesn't seem to be possible right now, but Microsoft hasn't updated its response in two years. Allow export of Teams chat history (13,685 upvotes): A similar request is present in our previous coverage, but Microsoft says that the ask is still under review, despite vehement support. Enable conversations to move to different channels (13,540 upvotes): In our 2022 coverage, Microsoft suggested workarounds to implement this behavior and its response is almost the same right now. The company is still asking for feedback about why you would want this feature, and people aren't too happy with Microsoft's pace on the issue. Enable access to email from left tab panel (12,265 upvotes): Interestingly, Microsoft has flat out declined this request after internal consideration. That's all for now. It is great to see that many items from our 2022 have been completed and rolled out to customers but the feature requests in this list do indicate that Microsoft sometimes takes years to implement Teams functionality even when the demand is seemingly high. In fact, it can even decline highly-upvoted customer requests without providing detailed justification. What feature do you think is still missing in Microsoft Teams? Let us know in the comments section below and let Redmond know your thoughts in the Feedback portal here!
    • only the copilot pc version went rtm in june. I think rtm was in september, so probably 2 months away.
    • 26200 25H2 is shaping up to be a very solid and stable release, hardly any issues with it here on 3 different configurations of computers
    • Samsung Galaxy S25+ 512GB is $220 off for powerful AI and a pro-grade camera by Paul Hill Are you in the market for a premium Samsung phone? If so, check out the Samsung Galaxy S25+ with 512GB of storage. It’s on Amazon right now for just $899, down 20% from its $1,199.99 list price, representing a significant $220.99 saving. This unlocked device is marked as the number 1 new release by Amazon in the Cell Phones category. If you’re interested, act fast as it’s a limited-time deal. The Galaxy S25+ comes packed with AI features under the umbrella of Galaxy AI. Capabilities include Multiple Tasks with One Ask which brings Google Gemini integration for multi-app commands, Now Brief which proactively gives you information you need to start the day, Audio Eraser to remove distracting sounds from your videos, and advanced portrait features. Powering these features is the Qualcomm SM8750-AB Snapdragon 8 Elite (3 nm) processor which handles all sorts of tasks efficiently including gaming, translation, and photo editing. Alongside the processor is 512GB of storage and 12GB of RAM. The S25+ uses a 6.7-inch QHD+ ProScaler Display which delivers vibrant visuals thanks to its use of Dynamic AMOLED 2X with 3,120 x 1,440 resolution and 120Hz refresh rate. Regarding camera setup, the S25+ has an AI camera with 50MP main sensor, 12MP ultrawide, and 10MP telephoto with OIS. There is also a 12MP front camera. This camera setup is capable of 8K video recording, which is impressive. Finally, you get long battery life with the 4,900 mAh and 45W fast charging support so you don’t need to wait long for it to recharge. If you’re an Android user looking to upgrade to a flagship phone without paying the full price, this deal is for you. If you have an eligible phone to trade in, there is an option to do so to claim up to $725 on the upgrade with Amazon.com Gift Card credit. If you’re excited by AI, but your current phone doesn’t support many AI features, this phone could also be a smart choice. Its display is also great for media consumption, and the processor is robust. Finally, if you have a lot of files to store, the 512GB of storage should be ample. Samsung Galaxy S25+ (Icyblue): $899 (Amazon US) / MSRP $1,199.99 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.
    • Sniffnet 1.4.0 by Razvan Serea Sniffnet is a network monitoring tool to help you easily keep track of your Internet traffic. Whether you want to gather statistics, or you need to inspect more in depth what's going on in your network, this app will get you covered. Sniffnet is a technical tool, but at the same time it strongly focuses on the overall user experience: most of the network analyzers out there are cumbersome to use, while one of Sniffnet's cornerstones is to be usable with ease by everyone. Furthermore, Sniffnet is completely free and open-source, dual-licensed under MIT or Apache-2.0: if you are interested you can find the full source code on GitHub. Last but not least, this application is totally developed in Rust: a modern programming language to build efficient and reliable software, emphasizing performance and safety. Sniffnet key features choose a network adapter of your PC to inspect select a set of filters to apply to the observed traffic view overall statistics about your Internet traffic view real-time charts about traffic intensity keep an eye on your network even when the application is minimized export comprehensive capture reports as PCAP files identify 6000+ upper layer services, protocols, trojans, and worms find out domain name and ASN of the hosts you are exchanging traffic with identify connections in your local network discover the geographical location of the remote hosts save your favorite network hosts inspect each of your network connections in real time set custom notifications to inform you when defined network events occur choose the style that fits you the most, including custom themes support ... and more! Sniffnet 1.4.0 changelog: New features Import PCAP files (#795 — fixes #283) Enhanced notifications (#830 — fixes #637) Donut chart reporting overall traffic statistics (#756 — fixes #687) Added support for ARP protocol (#759 — fixes #680) Identify and tag unassigned/reserved "bogon" IP addresses (#678 — fixes #209) Show data agglomerates in Inspect page table (#684 — fixes #601) Added Traditional Chinese (Taiwan) translation 🇹🇼 (#774) Added Indonesian translation 🇮🇩 (#611) A Docker image of Sniffnet is now available (#735) Improvements Added new themes A11y (Night) and A11y (Day) based on palettes optimized for Accessibility (#785 — fixes #786) Do not apply new notification thresholds while user is typing them (#777 — fixes #658) Show more information when domain name is short (#720 — fixes #696) Avoid directory traversal when selecting file name for PCAP exports (#776 — fixes #767) Add icon to window title bar (#719 — fixes #715) Update footer buttons and links (#755 — fixes #553) Handle errors to reduce the number of possible crash occurrences (#784) Updated some of the existing translations to v1.3: Portuguese (#690) Ukrainian (#692) Spanish (#805) Fixes Fix crates.io package for Windows (#718 — fixes #681) Fix crash when inserting characters longer than one byte in the text input for byte threshold notification setting (#747 — fixes #744) Remove pre-uninstall script on Linux (fixes #644) Fix typo in Russian translation (fixes #730) Minor fix to service determination algorithm in case of multicast and broadcast traffic Download: Sniffnet 64-bit | Sniffnet 32-bit ~15.0 MB (Open Source) Link: Sniffnet Home Page | Other operating systems | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • 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
      567
    2. 2
      +FloatingFatMan
      189
    3. 3
      ATLien_0
      176
    4. 4
      Skyfrog
      112
    5. 5
      Xenon
      110
  • Tell a friend

    Love Neowin? Tell a friend!