Click here to Skip to main content
15,888,009 members
Articles / Programming Languages / C#
Tip/Trick

Don't Name Things NAME....

Rate me:
Please Sign up or sign in to vote.
4.95/5 (10 votes)
4 Aug 2013CPOL1 min read 18.9K   4   9
Why you shouldn't use reserved words

A Quick Lesson On How To Waste 10 Minutes ...

If you have a new puppy, and it pees in the corner of the room, the recommended thing to do is get a fresh newspaper, roll it up tightly, raise it high in air, annnnnd.... bring it down sharply on *your own head*, repeating the mantra "I should've watched puppy more carefully...." ten times... then clean up the pee....

I was putting together a Windows form in C#/VS and after dropping on a ListView, proceeded to add some columns and give them titles. One of the columns was for "Customer name", but space was tight so I decided to make the title "Name" ... and that's where the trouble started. You see I didn't give the title property the value "Name", **by mistake** I gave the *NAME* property the value "Name" ... and continued to build out the interface. A while later, when I started to add code and build, but of course it failed. Naturally, the error message I got was not especially helpful so much head scratching and digging ensued.

Image 1

When I clicked the error, I don't see the entire picture...

Image 2

That only jumps out at me when I started back tracking what I changed to see what happened...

Image 3

Image 4

Conclusion

Lesson (re-learned): Don't use reserved words when naming objects or controls, it's bad, and it will bite you. Whenever something goes wrong, look for the simplest solution first, then dig deeper. Remember, compilers are very good at doing what we tell them, so when you get build errors, the first thing to look at is what you have written, chances are you've done a sprinkle in a corner somewhere.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer SocialVoice.AI
Ireland Ireland
Allen is CTO of SocialVoice (https://www.socialvoice.ai), where his company analyses video data at scale and gives Global Brands Knowledge, Insights and Actions never seen before! Allen is a chartered engineer, a Fellow of the British Computing Society, a Microsoft mvp and Regional Director, and C-Sharp Corner Community Adviser and MVP. His core technology interests are BigData, IoT and Machine Learning.

When not chained to his desk he can be found fixing broken things, playing music very badly or trying to shape things out of wood. He currently completing a PhD in AI and is also a ball throwing slave for his dogs.

Comments and Discussions

 
QuestionGood Tip! Pin
Johnny J.4-Aug-13 22:10
professionalJohnny J.4-Aug-13 22:10 
AnswerRe: Good Tip! Pin
Joezer BH4-Aug-13 22:11
professionalJoezer BH4-Aug-13 22:11 
AnswerRe: Good Tip! Pin
DataBytzAI4-Aug-13 22:22
professionalDataBytzAI4-Aug-13 22:22 
QuestionIts simple Pin
Vijayu 23-Aug-13 17:31
professionalVijayu 23-Aug-13 17:31 
When you add a textbox to a form with name propery set as 'Name', the form class(designer.cs) will be added with a new member 'Name'

Something like this...

private System.Windows.Forms.TextBox Name;

Inside InitializeComponent function the Form1's properties are initialized. Form1 has already a member named 'Name' which is a string. Now this is the problem, You have a string member called 'Name' and you have added one more member to this class with the same name - 'Name'. When you try to initialize, it got confused, whether initialization is for the string property or the control.
So
this.Name = "Form1"; will never work. See above highlighted text says its a textbox and you are trying to assign a string value to it.

Try this, name your text box 'ClientSize'
(private System.Windows.Forms.TextBox ClientSize;)
compiler will give the following error
'Cannot implicitly convert type 'System.Drawing.Size' to 'System.Windows.Forms.TextBox'

Bottom line; don't name your controls (which you are trying to add to a container) with the name of the property of the container itself. Ideally, VStudio should give you a warning, when you try to do this...
AnswerRe: Its simple Pin
Joezer BH4-Aug-13 22:10
professionalJoezer BH4-Aug-13 22:10 
GeneralThoughts Pin
PIEBALDconsult3-Aug-13 5:51
mvePIEBALDconsult3-Aug-13 5:51 
GeneralRe: Thoughts Pin
DataBytzAI3-Aug-13 6:06
professionalDataBytzAI3-Aug-13 6:06 
GeneralRe: Thoughts Pin
HaBiX4-Aug-13 6:43
HaBiX4-Aug-13 6:43 
GeneralRe: Thoughts Pin
DataBytzAI5-Aug-13 1:30
professionalDataBytzAI5-Aug-13 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.