Naming Conventions


To make code more self-documenting (always an important goal) and to reduce the chance of programming errors, you need an easy way to determine the exact data type of a variable or the exact type of a referenced control in Visual C# code.

Using Prefixes to Denote Data Type

Table 11.4 lists the prefixes of the common data types. Although you don't have to use prefixes, there are many advantages to be gained by doing so.

Table 11.4. Prefixes for Common Data Types

Data Type

Prefix

Sample Value

bool

bln

blnLoggedIn

byte

byt

bytAge

char

chr

chrQuantity

decimal

dec

decSalary

double

dbl

dblCalculatedResult

float

flt

fltInterestRate

int

int

intLoopCounter

long

lng

lngCustomerID

object

obj

objWord

short

sho

shoTotalParts

String

str

strFirstName


By the Way

The prefix of obj should be reserved for when a specific prefix isn't available. The most common use of this prefix is when referencing automation libraries of other applications.


Denoting Scope Using Variable Prefixes

Prefixes are useful not only to denote data types, they also can be used to denote scope (see Table 11.5). In particularly large applications, a scope designator is almost a necessity. Again, Visual C# doesn't care whether you use prefixes, but consistently using prefixes benefits you as well as others who have to review your code.

Table 11.5. Prefixes for Variable Scope

Prefix

Description

Example

g

Global

g_strSavePath

m

Private to class

m_blnDataChanged

(no prefix)

Nonstatic variable, local to method

fltInterestRate


Prefixes aren't just for variables. All standard objects (including forms and controls) can use a three-character prefix. There are simply too many controls and objects to list all the prefixes here, although you'll find that I use control prefixes throughout this book.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net