• 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

    • Or…because it makes it obvious what they are referring to. Imagine if Microsoft called it Visual Studio 22.
    • Ghost Downloader 3 v3.5.13 by Razvan Serea Ghost Downloader 3 is a high-performance download manager for Windows, featuring AI-enhanced acceleration, intelligent multi-threading, and smart file segmentation. It supports download resumption via .ghd files, global speed limits, and clipboard monitoring for quick link capture. The interface is built with Fluent Design, offering a modern and smooth user experience. Users benefit from advanced features like sparse file support, system tray integration, proxy configuration, secure SSL options, and automatic filename recognition. Ghost Downloader 3 is ideal for users seeking speed, efficiency, and customization. Core Download Features AI-Powered Acceleration: Experimental feature that dynamically increases thread count (up to 253) to maximize bandwidth usage. Intelligent Segmentation: Divides downloads into multiple parts for parallel processing, supporting breakpoint resume via .ghd files. Dynamic Thread Allocation: Automatically splits faster segments to create new threads, enhancing download speeds. Network & Proxy Support Global Speed Limiting: Allows setting a maximum download speed to manage bandwidth usage. Proxy Support: Compatible with SOCKS5, HTTP, and HTTPS proxies, including auto-detection features. SSL Certificate Verification: Optional SSL verification for secure downloads. System Proxy Detection: Automatically detects system proxy settings across Windows, Linux, and macOS. Windows-Specific Features Fluent Design UI: Modern interface with support for Mica, Acrylic, and Aero background effects. Toast Notifications: Supports Windows 10 (1709+) native notifications. Window Border Accent Colors: Enhanced visual integration with Windows 11. Application Features Smart Filename Recognition: Automatically identifies and decodes filenames from HTTP headers, URL parameters, or paths. Sparse File Support: Utilizes sparse file technology on supported file systems (e.g., NTFS) for efficient disk space allocation. Clipboard Monitoring: Optionally monitors clipboard for download links to facilitate quick task additions. Drag-and-Drop and Paste-to-Add: Supports intuitive methods for adding download tasks. Task Management: Features batch addition, per-task thread customization, pause/resume/cancel options, and hash verification (MD5, SHA1, SHA256). System Integration System Tray Support: Minimizes to system tray with background download capabilities. Automatic Startup and Task Resumption: Configurable to launch on system startup and resume unfinished downloads. Single Instance Enforcement: Prevents multiple instances from running simultaneously. Automatic Update Checks: Optionally checks for new versions on startup. Ghost Downloader 3 v3.5.13 changelog: Improved shared memory handling for macOS ARM by @cy920820 in #176 Fixed empty thread creation bug by @Alpha-Qian in #184 Optimized automatic speed boost feature by @Alpha-Qian in #183 Optimized download engine code by @XiaoYouChR Notes: Ghost Downloader may trigger a warning or show a hit on VirusTotal, but if only a few antivirus engines flag it, it’s likely a false positive. The app is open source, so you can inspect the code yourself for peace of mind. If you want to use Ghost-Downloader-3 on Windows 7, please download the version v3.5.8-Portable. Download: Ghost Downloader 64-bit | Portable 64-bit | ~50.0 MB (Open Source) Download: Ghost Downloader ARM64 | Portable ARM64 Links: Ghost Downloader Home Page | MacOS / Linux | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Microsoft Edge is getting new media control center, AI-powered history search, and more by Taras Buria Microsoft has updated Edge Beta to version 138. Its changelog contains many new features and improvements, some of which are quite interesting. For starters, Microsoft Edge is getting a media control center. This feature is intended to let you control multiple media sources from any website in a single place. The media control center button will show up on the toolbar (a button with a note icon), allowing you to launch picture-in-picture, cast media to other devices, control music or video, and more. Next is an AI upgrade for the History section. It now uses artificial intelligence to find relevant websites in your history. From now on, you do not need to remember the exact name of the website or a word from the page's name. You can find what you need using synonyms, phrases, or words with typos. Microsoft says this feature uses an on-device model that is trained on your data and does not send any information to Microsoft. AI-powered history search is rolling out gradually to Edge Beta insiders. Another interesting change is the dynamic Settings menu. If the browser slows down, a notification from Performance and Extensions Detector will show up in the menu to help users learn about the built-in methods to improve performance. Like AI-powered history search, this feature is rolling out gradually. Other changes in Microsoft Edge 138 Beta include the following: Microsoft Edge can now use your primary work profile by default when opening external links instead of the last used profile (rolling out gradually). Autofill received a new toggle that lets users consent to Microsoft Edge collecting web form field labels to improve suggestion accuracy. Note that this feature does not send user-entered data; it only sends field labels. Enterprise users can now view sensitivity labels in MIP-protected PDFs. Microsoft 365 Copilot Chat Summarization is now available in the context menu (gradual rollout). As usual, each major browser release delivers policy updates. You can find the complete list of policy changes in the release notes. Microsoft Edge Beta 138 is now available for download. You can get it on the official Edge Insider website. Version 138 will be available in the Stable Channel on the week of June 26, 2025.
    • Winxvideo AI V3.0 lifetime license for PC (worth $69.95) free offer ends June 8 by Steven Parker Claim your complimentary license worth $69.95 for free today, before the offer ends on June 8. Powered by CineAI technology, Winxvideo AI can revitalize your video/photo, whether it’s old, low in resolution, noisy, or blurry; and it also can upscale, stabilize, convert, compress, record, and edit 4K/8K/HDR videos smoothly and efficiently, achieving cinema-grade visuals in every frame. On occasion of WinXDVD's 19th anniversary, everyone here can get a lifetime license of Winxvideo AI V3.0 for free as a thank-you gift. Grab the chance to enhance and process your multimedia content effortlessly, at no cost. Claim your free license before 6/8/2025. What you can do with Winxvideo AI: AI video enhance: upscale low-quality video to 1080P or 4K; stabilize shaky video shot by Gopro, drone, handheld camera or mobile phone; increase frame rate up to 480 fps. Smoother, steadier and crisper playback. AI image enhance: upscale and restore old, blurry image to 4K/8K/10K with real details. Convert any video and music to 420+ formats for various use. GPU accelerated. Compress 8K/4K/HD video to smaller size with highest quality. Record tutorials, gameplay videos, and more from screen, webcam, or both. Download: save 4K/1080P video, music, playlist, channel, m3u8, etc., from 1000+ sites for offline playback. Edit video: cut, crop, merge, flip, rotate video; add effect/watermark/subtitle, make GIF, change playback speed, etc. Free offer expires June 8. How to get it Please ensure you read the terms and conditions to claim this offer. Complete and verifiable information is required in order to receive this free offer. If you have previously made use of these free offers, you will not need to re-register. While supplies last! Download Winxvideo AI V3.0 Lifetime License for PC (worth $69.95) now FREE Offered by Wiley, view other free resources The below offers are also available for free in exchange for your (work) email: Winxvideo AI V3.0 Lifetime License for PC ($69.95 Value) FREE – Expires 6/8 Aiarty Image Enhancer for PC/Mac ($85 Value) FREE – Expires 6/8 Solutions Architect's Handbook, Third Edition ($42.99 Value) FREE – Expires 6/10 AI and Innovation ($21 Value) FREE – Expires 6/11 Unruly: Fighting Back when Politics, AI, and Law Upend [...] ($18 Value) FREE - Expires 6/17 SQL Essentials For Dummies ($10 Value) FREE – Expires 6/17 Continuous Testing, Quality, Security, and Feedback ($27.99 Value) FREE – Expires 6/18 VideoProc Converter AI v7.5 for FREE (worth $78.90) – Expires 6/18 Macxvideo AI ($39.95 Value) Free for a Limited Time – Expires 6/22 The Ultimate Linux Newbie Guide – Featured Free content Python Notes for Professionals – Featured Free content Learn Linux in 5 Days – Featured Free content Quick Reference Guide for Cybersecurity – Featured Free content We post these because we earn commission on each lead so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin The above deal not doing it for you, but still want to help? Check out the links below. Check out our partner software in the Neowin Store Buy a T-shirt at Neowin's Threadsquad Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: An account at Neowin Deals is required to participate in any deals powered by our affiliate, StackCommerce. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through the branded deals site.
  • Recent Achievements

    • Week One Done
      abortretryfail earned a badge
      Week One Done
    • First Post
      Mr bot earned a badge
      First Post
    • First Post
      Bkl211 earned a badge
      First Post
    • One Year In
      Mido gaber earned a badge
      One Year In
    • One Year In
      Vladimir Migunov earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      495
    2. 2
      +FloatingFatMan
      253
    3. 3
      snowy owl
      251
    4. 4
      ATLien_0
      228
    5. 5
      +Edouard
      191
  • Tell a friend

    Love Neowin? Tell a friend!