• 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

    • spwave 0.9.0-1 by Razvan Serea spwave is a cross-platform audio editor designed for research and advanced analysis. It supports a wide range of audio formats, including WAV, AIFF, MP3, Ogg Vorbis, FLAC, ALAC, raw PCM, and more via plug-ins. spwave offers precise editing tools such as zoom, crop, fade in/out, gain adjustment, and region extraction. It enables detailed spectral and phase analysis and supports unlimited undo/redo. Users can drag and drop files, edit metadata, save labeled regions, and view multiple synchronized waveforms. Internally, spwave processes audio in 64-bit precision, ensuring high accuracy. It runs on Windows, macOS, and Linux, making it a reliable and flexible tool for audio research and editing. spwave has following features: Support for multiple platforms: Windows, macOS, Linux (Motif, gtk), etc. Support for WAV, AIFF, MP3, Ogg Vorbis, FLAC, ALAC, raw, and text files by using plug-ins. Support for many bits/samples: 8bits, 16bits, 24bits, 32bits, 32bits float, 64bits double. Converting the sampling frequency and the bits/sample of a file. Playing, zooming, cropping, deleting, extracting, etc. of a selected region. Fade-in, fade-out, gain adjustment, channel swapping, etc of a selected region. Editing file information that supports comments of WAV and AIFF, and ID3 tag of MP3. Analysis of a selected region using several analysis types, e.g. spectrum, smoothed spectrum, phase, unwrapped phase and group delay. Undoing and redoing without limitation of the number of times. Waveform extraction by drag & drop. Opening files by drag & drop. Autosaving of selected regions (you can do this by drag & drop also). Saving positions and regions as labels. Viewing some waveforms and setting regions synchronously. Almost all processing is 64 bits processing internally. Supported Formats: Read/Write: WAV, AIFF, AIFC, CAF, MP3, Ogg Vorbis, FLAC, ALAC (.caf, .mp4), WMA (Windows), APE, AU/SND, PARIS, NIST, IRCAM, raw PCM, text. Read-only: MPEG-2 Layer 3 MP3, RMP files with VBR support. With 64-bit internal processing, autosave capabilities, and synchronized multi-view waveform editing, spwave is a solid tool for anyone handling complex audio editing or acoustic research. spwave 0.9.0-1 changelog: Implemented CQT spectrum and CQT spectrogram (beta version). Implemented piano-key display for spectrum/spectrogram view. Implemented indication of musical note name in cursor information for spectrum/spectrogram view. Fixed a bug that spectrogram view after zoom-in with large factor sometimes freezes. Fixed a bug that scroll and zoom-out in spectrogram view after zoom-in with large factor do not work correctly. Fixed a bug that spectrogram view provides sometimes wrong time information. Fixed a bug that plugin errors sometimes cause a crash. Fixed a bug that the color of grid lines is wrong in printing. Optimized layout of spectrogram view for printing. Enhanced the function of waveform cropping from label information. Fixed a bug that some items in the preference dialog related to labels do not work. Added some items related to the region label in the preference dialog. Fixed a bug that drawing selected region in the log-frequency axis does not work correctly. Added partial support for the dark mode of Windows (the menu bar and the menus). Fixed a bug that the cursor to indicate current calculation position of spectrogram is sometimes not shown. Changed drawing of cursor information into that with white background so as to make the information legible. Fixed a bug that moving to the head by scrolling the overview display sometimes fails. Added feature of alignment of the view region between spectrum view and spectrogram view. Download: spwave 64-bit | spwave 32-bit | ~3.0 MB (Freeware) Download: spwave ARM64 | 2.9 MB Links: spwave Home page | Other OSes | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Microsoft Weekly: redesigned Windows 11 Start menu, Xbox handheld is here, and more by Taras Buria This week's news recap is here. Fresh Windows 11 preview builds with the redesigned Start menu and Windows Vista flashbacks, the long-anticipated Xbox handheld, Patch Tuesday updates, gaming news, and more. Quick links: Windows 10 and 11 Windows Insider Program Updates are available Gaming news Great deals to check Windows 11 and Windows 10 Here, we talk about everything happening around Microsoft's latest operating system in the Stable channel and preview builds: new features, removed features, controversies, bugs, interesting findings, and more. And, of course, you may find a word or two about older versions. June 2025 Patch Tuesday updates are out. Windows 10 received KB5060533 with build numbers 19044.5965 and 19045.5965. Supported Windows 11 versions received KB5060842 and KB5060999 with build numbers 26100.4349, 22631.5472, and 22621.5472. Later, Microsoft released an out-of-band update to address problems with games with Easy Anti-Cheat, causing system restarts upon launch, and a couple of recovery updates. Microsoft launched Copilot Vision with Highlights for Windows. This feature enables AI to see what is happening on the screen and offer additional information, analysis, and context. Copilot Vision currently works with up to two apps, but its availability is limited to the United States (more countries are on the way, says Microsoft). Now, here is some useful stuff for Windows users: a neat third-party maintenance tool that can run various checks, troubleshooters, and repair utilities; a useful guide about personalizing OneDrive folders with a touch of color, and more. Windows Insider Program Here is what Microsoft released for Windows Insiders this week: Builds Canary Channel Dev Channel Build 26200.5641 This build introduces the recently announced Start menu redesign. It also packs Lock Screen widget improvements, Narrator enhancements, updates to the gamepad keyboard, and a lot of various fixes. Build 26200.5651 Another Dev build introduced a Settings app agent, Recall improvements, seconds for the calendar clock, context menu enhancements, and more. Beta Channel Build 26120.4250 The Beta build has the same changelog as the one from the Dev Channel. Build 26120.4441 The same build as 26200.5651 from the Dev Channel. Release Preview Channel Build 22631.5545 With build 22631.5545 for Widnows 11, Microsoft improved default browser settings and the Windows Share UI and fixed several bugs. Build 19045.6029 This build introduces improvements to app defaults and multiple fixes for Windows 10. The redesigned Start menu is the most exciting part of the new builds, but as usual, it is rolling out gradually. You can mitigate that by force-enabling the new Start menu using the ViVeTool app as described in our guide. Interestingly, the latest builds introduced a funny bug where Windows 11 plays the Windows Vista startup sound on boot. Microsoft acknowledged the issue and said it is working on a fix in future updates. Meanwhile, if you use the latest Dev and Beta builds, you will get to enjoy 2006 nostalgia each time you turn on your PC. Updates are available This section covers software, firmware, and other notable updates (released and coming soon) delivering new features, security fixes, improvements, patches, and more from Microsoft and third parties. This week's browser updates include a fresh Dev Channel update for Microsoft Edge and secure password deployment in Edge for organizations. The latter arrived in the Stable Channel on June 13 with version 137.0.3296.83. There was also a minor update for Firefox. The latter received version 139.0.4, which addressed several issues with the browser freezing when switching apps, failing to save wallpapers with proper names, and more. In addition to the update, Mozilla announced that Deepfake Detector is shutting down. The service will go dark on June 26, 2025. Moving to Office updates, we have some changes to the new Outlook, which will block more files and allow you to perform more tasks when offline. OneDrive for Mac now supports external disks, Clipchamp lets you trim videos by cutting out parts of the transcript, and OneNote now supports Copilot Notebooks. Microsoft also announced an update on the removal of Exchange Online Basic Authentication in Office 365. Here are other updates and releases you may find interesting: Rufus received an update to version 4.8 with performance improvements for Windows images. Microsoft is committing to upskilling 1 million UK workers in AI this year. Here are the latest drivers and firmware updates released this week: Intel 32.0.101.6881 WHQL graphics driver with a single fix for Overwatch 2. AMD 25.6.2 non-WHQL with support for FBC: Firebreak, The Alters, and more. On the gaming side Learn about upcoming game releases, Xbox rumors, new hardware, software updates, freebies, deals, discounts, and more. A lot happened on the gaming side this week. At the Sunday Game Showcase, Microsoft and ASUS announced two Xbox handhelds: the ROG Xbox Ally and the ROG Xbox Ally X. These portable consoles are a big deal for the world of handheld devices, as they run a special version of Windows, which was optimized for portable gaming consoles with fewer processes running in the background. As such, they offer much better battery life and performance. You can read more about how Microsoft optimized Windows 11 for handhelds in a separate article. Next, we have plenty of new games and DLCs announced at the showcase; here is a recap: Indiana Jones and the Great Circle received a new DLC called The Order of Giants. It will be available on all supported platforms this September. Call of Duty: Black Ops 7 made a surprise appearance at the showcase. Activision released a teaser trailer where the game takes players to a futuristic experience set in 2035. Grounded 2 was announced. The sequel of the game for people with arachnophobia is coming next month, offering gamers a new miniaturized survival adventure. Obsidian Entertainment revealed the release date of The Outer Worlds 2 and details about companions. At Fate's End by Spiritfarer was announced, a new action game about fighting family. It is coming to consoles and PC somewhere in 2026. Skybound Games revealed Invincible VS, a brutal 3v3 tag fighting game by former Killer Instinct developers. Anno 117: Pax Romana received a November release date. Ubisoft also unveiled a special Governor's Edition. Nvidia announced new games for its cloud-streaming gaming service, GeForce NOW. If you own one of the following games, you can play them on Nvidia's cloud. The new additions include Frosthaven Demo, Dune: Awakening, MindsEye, The Alters, Kingdom Two Crowns, and more. Mojang finally has a release date for Vibrant Visuals and Chase the Skies updates. On June 17, Minecraft will get its long-anticipated visual overhaul, new features, fresh mobs, and more. Deals and freebies Steam is running a new Next Fest, during which gamers can try hundreds of games for free. The event ends on June 16, 2025. The Epic Games Store is giving away Two Point Hospital, a humorous hospital builder simulator. As usual, more deals are available in this week's Weekend PC Games Deals article. Other gaming news includes the following: GOG store introduced the One-Click Mods feature with support for Fallout: London and others. Valve announced new accessibility details for game listings on Steam. Steam finally has a native client for Apple Silicon. To finish this week's gaming section, here is an editorial from Paul Hill exploring the new $80 cost frontier in modern gaming. Great deals to check Every week, we cover many deals on different hardware and software. The following discounts are still available, so check them out. You might find something you want or need. JBL Bar 1000 and 700 sound bars Ring Floodlight Cameras Geekom Mini IT12 mini PC - $499 | $200 off Amazon Kindle Scribe (16GB) - $299.99 | 25% off LG gram Pro 16" Copilot+ PCs - $1,499.99 | 25% off GameSir Super Nova Wireless Controller for PC and mobile - $44.99 | 25% off Intel Core Ultra 7 Desktop Processor 265K 5.5 GHz - $259.99 | $144 off 12TB Seagate IronWolf Pro HDD - $218.49 | 13% off Polk Audio React 7" Wireless Subwoofer - $99.99 | 50% off StreamMaster Plus2 4K Gaming Projector - $1,699 | 15% off AMD Ryzen 5 9600X - $179.99 | 35% off Sony BRAVIA 5 65 Inch TV Mini LED - $1,298 | 13% off This link will take you to other issues of the Microsoft Weekly series. You can also support Neowin by registering a free member account or subscribing for extra member benefits, along with an ad-free tier option. Microsoft Weekly image background by
    • Yea but you cant forget about Windows 7 featuring most of this design too... it was also there in Win7!
    • I don't blame them, just have a load of people stand in front of the cars, they will not or should not move, that will stuff them.
    • Yeah, that would work. I have no problem with the way macOS looks like now, I don't see the point of them changing the look all the time, and that is the same with windows. I realise look have to change sometimes, but macOS is fine as it is, look wise.
  • Recent Achievements

    • Apprentice
      Wireless wookie went up a rank
      Apprentice
    • Week One Done
      bukro earned a badge
      Week One Done
    • One Year In
      Wulle earned a badge
      One Year In
    • One Month Later
      Wulle earned a badge
      One Month Later
    • One Month Later
      Simmo3D earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      593
    2. 2
      ATLien_0
      277
    3. 3
      +FloatingFatMan
      181
    4. 4
      Michael Scrip
      148
    5. 5
      Steven P.
      111
  • Tell a friend

    Love Neowin? Tell a friend!