Declaring and Referencing Variables


Variables are similar to constants in that when you reference a variable's name in code, Visual C# substitutes the variable's value in place of the variable name during code execution. This doesn't happen at compile time, though, as it does with constants. Instead, it happens at runtimethe moment the variable is referenced. This is because variables, unlike constants, can have their values changed at any time.

Declaring Variables

The act of defining a variable is called declaring. (Variables with scope other than local are dimensioned in a slightly different way, as discussed in the section on scope.) You've already defined variables in previous hours, so a declaration statement should look familiar to you:

datatype variablename = initialvalue;


You don't have to specify an initial value for a variable, although being able to do so in the declaration statement is very cool and useful. To create a new string variable and initialize it with a value, for example, you could use two statements, such as the following:

string strName; strName = "Matt Perry";


However, if you know the initial value of the variable at design time, you can include it on the declaration statement, like this:

string strName = "Matt Perry";


Note, however, that supplying an initial value doesn't make this a constant; it's still a variable, and the value of the variable can be changed at any time. This method of creating an initial value eliminates a code statement and makes the code a bit easier to read because you don't have to go looking to see where the variable is initialized.

It's important to note that Visual C# is a strongly typed language; therefore, you must always declare the data type of a variable. In addition, Visual C# requires that all variables be initialized before they're used.

By the Way

Visual Basic programmers should note that Visual C# will not default numeric variables to 0 or strings to empty strings.


For example, the following statements would result in a compiler error in Visual C#: Use of unassigned local variable 'fltMyValue'.

float fltMyValue; System.Diagnostics.Debug.WriteLine (fltMyValue + 2);


By the Way

You can't use a reserved word to name a constant or a variable. For example, you couldn't use the word private or public as a variable name. There is a master list of reserved words, and you can find it by searching the Help text for public Keyword. You'll naturally pick up most of the common ones because you'll use them so often. For others, the compiler will tell you when something is a reserved word. If you use a naming convention for your variables, which consists of giving the variable names a prefix to denote their type, you'll greatly reduce the chance of running into reserved words.


Passing Literal Values to a Variable

The syntax of assigning a literal value (a hard-coded value such as 6 or "guitar") to a variable depends on the data type of the variable.

For strings, you must pass the value in quotation marks, like this:

strFirstName = "James Tyler";


There is one caveat when assigning literal values to strings: Visual C# interprets slashes (\) as being a special type of escape sequence. If you pass a literal string containing one or more slashes to a variable, you'll get an error. What you have to do in such instances is preface the literal with the symbol @, like this:

strFilePath = @"c:\Temp";


When Visual C# encounters the @ symbol after the equal sign like this, it knows to not treat slashes in the string as escape sequences.

To pass a literal value to a char variable, use single quotes instead of double quotes, like this:

chrMyCharacter = 'j';


For numeric values, you don't enclose the value in anything:

intAnswerToEverything = 42;


Using Variables in Expressions

Variables can be used anywhere an expression is expected. The arithmetic functions, for example, operate on expressions. You could add two literal numbers and store the result in a variable like this:

intMyVariable = 2 + 5;


In addition, you could replace either or both literal numbers with numeric variables or constants, as shown next:

intMyVariable = intFirstValue + 5; intMyVariable = 2 + intSecondValue; intMyVariable = intFirstValue + intSecondValue;


Variables are a fantastic way to store values during code execution, and you'll use variables all the timefrom performing decisions and creating loops to using them as a temporary place to stick a value. Remember to use a constant when you know the value at design time and the value won't change. When you don't know the value ahead of time or the value might change, use a variable with a data type appropriate to the function of the variable.

Did you Know?

In Visual C#, variables are created as objects. Feel free to create a variable and explore the members (that is, the properties and methods) of the variable. You do this by entering the variable name and pressing a period (this works only after you've entered the statement that declares the variable). For example, to determine the length of the text within a string variable, you can use the Length property of a string variable like this:

strMyVariable.Length


There are some powerful features dangling off the data type objects.





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