Maximum PC

It is currently Sun May 03, 2015 4:11 pm

All times are UTC - 8 hours

Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: C# naming conventions
PostPosted: Sat Jun 26, 2010 5:33 pm 

Joined: Tue Feb 01, 2005 12:17 pm
Posts: 1522
Is there a set of "socially used" naming conventions that people tend to use in C#, when dealing with naming of controls, forms, classes, modules, etc?

I haven't really done anything serious in c# just yet, so I want to start a good method when I do write programs in C#.

In the past, for VB, I've use things like this (here are the prefixes that I've seen used a lot for VB, for example):

frm = Form
txt = Textbox
lbl = Label
btn (or cmd) = Pushbutton
chk = Checkbox
opt = Radio Button / Option
and the list goes on....

Additionally, what about for naming normal variables (not constants)? I've use the following types for my naming conventions in the past with VB:

int = integer
dbl = double
sng = single
str = string
chr = character
byt = byte
bool = boolean

Any suggestions? (I know everyone has their own specific programming/coding styles, but is there a comon practice that's used so that in general, programmers can interpret what each component is in source code?)

 Post subject:
PostPosted: Sat Jun 26, 2010 5:37 pm 

Joined: Tue Feb 01, 2005 12:17 pm
Posts: 1522
I'm aware of some of the common naming conventions from other languages...

such as using all CAPS for constants (and possibly for enumerations), and the use of the Pascal-naming style for functions and classes (First letter of each word in a name is capitalized, as in "DisplayResult").

 Post subject:
PostPosted: Mon Jun 28, 2010 9:21 am 
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
I don't have a formal definition that I adhere to, but I am as consistent as possible in each application.

I don't prefix my variables with the type... usually.

ints usually get an n. Bools a b, doubles a d, ect.

Objects, depending on the project is usually o or t_.

I typically put the control type at the end, except for forms.

Textbox, TB... ex: UserNameTB.
GridView, GV... ex: CommentsGV.

It makes it easier to fully use intellisense. You don't have to think about the control, just know what you are putting in it, if it is named properly finding the controls is easy. Then the type of control becomes clear with the postfixed tag.

 Post subject: Re: C# naming conventions
PostPosted: Mon Jun 28, 2010 12:15 pm 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 988
Location: Earth
I don't prefix my types at all, I try to be as descriptive with my variable names as possible. For example

bool isAdmin;
int sumOfBillsPaid;

For method names, I use the following:

For controls in WinForms or ASP.NET:

TextBox tbUserName;
DropDownList ddlOptions;
CheckBox cbIsAdmin;
RadioButton rbGender;
ListBox lbxSelectedOptions;
CheckBoxList cblManyOptions;
RadioButtonList rblManyOptionsAlso;
GridView gvwMyGrid;
Button btnSubmit;
Label lblUserMessage;

I'll go even one further, with SQL Server:

Stored Procedures;



vw_TableName_NameOfView or vwNameOfView (usually the first)

if abstraction is involved then:

vw_TableName_Lx_NameOfView (where Lx is a level: Level 0 -> Level n)



I adhere to these strict naming conventions and apply them everywhere.

 Post subject:
PostPosted: Mon Jun 28, 2010 1:19 pm 

Joined: Tue Feb 01, 2005 12:17 pm
Posts: 1522
I don't know. It goes back to when I was doing VB3 or 4, and I've done it ever since. Even when I was doing console programming with C/C++ I'd still do it (you know, "int" for integer, "sng" for single, "str" for string, etc).

I like doing it because I don't have to worry about forgetting what variable is what. Although with most languages these days (and probably way back when) with the idea of promotion and demotion with numerical data types, I guess it doesn't matter that much, as long as you are descriptive. But even being descriptive sometimes isn't enough.

For example, Date of Birth... Some may store it as a stringed value, whereas others may actually use a Date data type (if it's available in the language, which it is in most). So, someone else might not know whether the variable "DOB" (for example) takes a string or a date.

I don't know. Maybe it's just me, but I like my code to be readable so others can read it if they need to. Occassionally, when I'm in a hurry, I'll do simple stuff that might not be as straightfoward, for example just using single letters for variable in LOOPing structures.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours

Who is online

Users browsing this forum: No registered users and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.