Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"this" in C# like "me" in VB.NET?

Status
Not open for further replies.

JGALEY

IS-IT--Management
May 21, 2003
105
US
Hello, this is probably a real newbie question, but I could use a bit of advice.

I have some experience in VB.NET, and one of the things I like is that I can put "me." in the code, and all the objects on my forms (text boxes, labels, list boxes, etc) show up in an intellisense list. It makes it easy to code, and I love it.

I am pretty new to C#, and I see that if I use "this." in the code, I get the intellisense list.

My question: is this a good practice? Are there potential problems, and should I suck it up and just type out the name of the form objects I am using?

It seems like "this" and "me" are not exactly equal in terms of what they do and what they mean, but I am not sure.

Thanks in advance,

Jason
 
this" and "me" are equal.

I prefer not to use them for inline statements as I don't like the way it looks, however you may need to remove ambiguity.

I don't know of any pitfalls in using "this" in c#.

There are other places that it's useful which these articals explain.

me

this
 
Yeah, sometimes you need to use "this" to specify the member of the class, and not a parameter by the same name.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for the links and the advice! I am a real intellisense junky, so I will probably keep using "this." :)

Jason
 
You have to use intellisense if you want to be fast.

control+spacebar completes a word or shows the intellisense.
 
Although I used it in VB.NET, for some reason I was thinking that control+spacebar did not work in C#. But now I see that it does. Cool!

Thanks,

Jason
 
If you prefix your controls txtUserName for a textbox lblSite for a label, etc you would have a much smaller list to choose from when typing txt <cntrl><spacebar> which would show you all of your textboxes typing this. could give you big list and slow you down.
I also believe it should be used as Chiph stated, and the link that stsuing provided states how it should be used. I would not want to maintain code that over used the keyword as you intend to do.
Marty
 
Cappmgr, using prefixes like txt, lbl, etc. is no longer considered good form under .NET. Check out Chip's faq: faq732-3158. Also check out this summary which also has a piece at the end that describes some of the reasons why the recommendations have changed.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

I was not born cynical - I earned my cynicism through careful observation of the world around me.
 
MasterRacker, tyvm for the feedback. I quickly perused the faq and saw no mention of ui controls on MSDN. I did see it on the other link to prefix all ui controls with ux. I'll buy that and see if I can run it buy the team as a standard on our next app.
I stand buy what I stated about the keyword this.
Thank again,
Marty
 
Jeez, ANOTHER thing I have been doing wrong!!! :) I was reading the summary link, and I do have to agree that the prefixes become counter-productive at some point, especially in databases. Yup, I was taught to use "tbl" or "t" in front of tables, "proc" or "p" in front of stored procedures.

I think I can go for using "ux" in front of form controls.

Thanks,

Jason
 
The one thing that never changes is change. ;-)

I've been using the ux prefix in a curernt project and like it. Takes some getting used to though. I learned the same lbl, txt, etc. years ago doing a lot of Access work.

The new .NET guidelines make a lot of sense though. Any decent IDE will give you type info so it make a lot more sense to focus on what something is or what it's doing.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

I was not born cynical - I earned my cynicism through careful observation of the world around me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top