Declaring Variables In programming lingo a literal value is a value that is entered by hand such as: 3.5, 50, or even a string like "Hello World." (String literal values are enclosed in quotation marks.) Sometimes you will use literal values when calling functions ( Figure 2.12 ) or when setting the property of a control ( Figure 2.13 ). But most of the time you'll use variables and constants. (We'll discuss constants below.) Figure 2.12 Many times you can pass literals as parameters when calling functions. However, most of the time it is preferable to use variables or constants (more on this later). AddTwoNumbers( 4.5 , 10.7 ); Figure 2.13 To represent a string in literal form put quotes around it. TextBox1.Text = "Hello World" ; A variable serves as a container. The container can store a value, such as a number, or a memory address where an object resides, depending on the type of variable. Some variables can be set to a literal value ( Figure 2.14 ) or to the contents of another variable ( Figure 2.15 ). Variables have a type. For example, they could be integers, strings, etc. Figure 2.14 A variable can be used to store a value or an object. Then you can use the variable instead of the actual value throughout your code. string LastName = "Smith"; Figure 2.15 A variable can point to another variable. You might use this to store the value of another variable before changing it. int count = 5; int lastCount = count; count = 6; To declare a variable: -
Type the name of the class of which you wish to declare a variable. You could choose a type from Table 2.1 oras you will see lateryou could also use a class name for the variable type. The class name can come from a class you declare, or from a class that someone else declared and which you're referencing in your project. -
Add a space followed by the name of the variable. The variable name must start with a letter, or an underscore . -
Type a semicolon ( Figure 2.16 ). Figure 2.16 Variables are declared by first writing the type of the variable, then the name of the variable. There are a number of predefined numeric types. Notice that sometimes a letter is required at the end of a numeric value when assigning the value to the variable. bool isOpened; byte age; int numRecords; float rate = .33F; //use F at the end for floats double sinAngle = 0.70710678; decimal balance = 1072.45M; //use M at the end for decimals string firstName = "Jose"; string lastName = "Mojica"; string fullName = firstName + " " + lastName; Table 2.1. Core Types (Most Commonly Used Types) NAME | C# TYPE | RANGE | Boolean | bool | True/False | Unsigned Byte | byte | 8-bit unsigned integer, 0 to 255 | Signed Byte | sbyte | 8-bit signed integer -128 to 127 | Short | short | 16-bit integer, -32768 to 32767 | Unsigned Short | ushort | 16-bit unsigned integer 0 to 65535 | Integer | int | 32-bit integer, -2147483648 to 2147483647 | Unsigned Integer | uint | 32-bit unsigned integer 0 to 4294967295 | Long | long | 64-bit integer, -9223372036854775808 to 9223372036854775807 | Unsigned Long | ulong | 64-bit unsigned integer 0 to 18446744073709551615 | Character | char | A 16-bit Unicode Character (a single letter). | Single | float | A 32-bit floating point number with 7-8 points of precision -3.402823E+38 to 3.402823E+38 | Double | double | A 64-bit floating point number with 15-16 points of precision -1.79769313486232E+308 to 1.79769313486232E+308 | Decimal | decimal | A 96-bit fixed point number with 28 points of precision. A very big number. Use decimal to store currency. | Date/Time | System.DateTime | A 64-bit integer in IEEE date format, 1/1/0001 12:00:00 AM to 12/31/9999 11:59:59 PM | String | string | A fixed array of characters . | Object | object | An object variable can be set to anything, including numeric types and objects created from classes. | Tips -
If you're declaring more than one variable of the same kind, type a comma followed by the next variable name, before the semicolon ( Figure 2.17 ). Figure 2.17 You can declare multiple variables of the same type in one statement. You can even initialize the variables at the same time you declare them. byte x,y,z; int numMen=10, numWomen=20, people=numMen+numWomen; -
When you compile code that includes a variable declaration, the definition for the variable type must be available to the compiler. The types in Table 2.1 are part of a system DLL called mscorlib.dll. This DLL is automatically referenced in all projects. If the type used isn't one you declared by hand and it isn't one in mscorlib.dll, you must add a reference to your project for the DLL that contains the class definition. Files that contain .NET code are called assemblies. An assembly is the smallest package of code that the .NET runtime will execute. For details, see "Including Class Definitions from Outside Sources" later in this chapter. -
Variables are only declared inside of a class, as part of a function definition, or as part of the body of a function. Classes and functions are defined later in this chapter. Variables that are declared inside of a class are called fields ; Variables that are used to define a function are called parameters ; Variables that are declared inside of a function are called local variables ( Figure 2.18 ). Figure 2.18 Fields are variables declared inside classes, parameters are variables used in function declarations and local variables are variables declared inside functions. You can't declare variables outside of classes. class ToDoItem { bool active; //field string description; //field //parameter void Complete(string finalComments) { //local variable DateTime now = DateTime.Now; active = false; description = description + now.ToString() + finalComments; } } |