Wednesday, November 14, 2007

Variables

Creating Variables

Variables are very important in the C# language. Using variables gives you control of many different values. There are tons of different variables that can be used in C# and they all follow a similar pattern; one that we will explain.

First of all, what is a variable? A variable is named object that can be accessed and manipulated. You can access these values to use them in your project.

There are three different ways you can declare variables. The first way you can declare a variable is at the top of a class. This allows you to access the variable from any method or property inside of the class. These types of variables can be called class-variables. To declare a class variable, you would try and follow this format:

accessModifier variableType variableName ;

First of all, there is the access modifier. An access modifier is a keyword placed in front of a variable, method or other member, which restricts or allows access to it from some other class or struct. A member is any variable, property, or method inside of a class. The private modifier allows the least amount of access and allows the item to only be accessed inside the class it was created in. The protected modifier allows access only from inherited classes, and the public modifier allows total access from all classes.

There is also an access modifier named static. The static keyword allows the item it is being applied to, to not be part of a class instance but, instead, related to the class name. This modifier is not used at the moment but will be explained later.

After the access modifier we have the variable type. Each variable type contains different information needed for that variable type. For example, the int class contains information that holds integers. The string class contains information that holds text, and the bool class contains information that holds true or false values. Each variable type is different but most can be created the same way.

Then, after the variable type, you can name the variable. Variable names can be anything they want, but they are case-sensitive.

Finally, you have the semi-colon which you place to signify that you are done creating the variable.

As I told you, there are different ways that you can create a variable. We just looked at the class variable type. There is also another way to create variables. In this way, you create a variable inside a method or property. The difference between these types of variables is that they can only be accessed inside of that method or property. You create these types of variables the same way as a class variable, but you do not place an access modifier because you can’t access the variable outside of the function it was created in. This limit of access is called a variables scope. You would want to create this type of variable like so:

variableType variableName ;

Finally, there is one last way you can create variables. These types of variables are used inside method calls and are called parameters. For example if I called the, Multiply method of a Math class then I could pass in, 5 as an parameter. I wouldn’t name the variable because I can never access it again, and I don’t need any access modifiers, variable type name, or even a semi-colon. All I need is the value of the variable.

There are many different types of variables and their uses can be used in different ways. Here is a list giving a quick description of the variable types and their uses:

Variable Type; Containment Type; Constructor Type
Int32(int); (-)2,147,483,64(7-8); int intVar;
Int64(long); (-)9,223,372,036,854,775,80(7-8);long longVar;
Byte(byte); 255; byte byteVar;
UInt32(uint); 4,294,967,295; uint uintVar;
Uint64(ulong); 18,446,744,073,709,551,615; ulong ulongVar;
Single(float); (-)7 Digits; float floatVar
Double(double); (-)15-16 Digits; double doubleVar;
Decimal(decimal); (-)28-29 Digits; decimal decimalVar;
String(string); Text between quotes(“”); string stringVar;
Char(char); One Character between symbols(‘’);char charVar;
Boolean(bool); True or False; bool boolVar;
Random; Used for Randomizing; Random rnd = new Random()


The Constructor Type shows how you can create a variable of that type. The Random class inherits from the Object class instead of the Type class and needs to be created using the, new ClassType(...); format. This way calls the constructor of that variable. The constructor of a variable is the method that is called to create the variable.

Assigning Values to Variables
Assigning values to variables is also very important. In order to actually be able to use the variable in your project, you need a way to assign values to it.

The assignment operator is used for assigning values. An operator is a symbol that C# recognizes as a command to do something to a variable(s). You can assign variables using the following structure:

leftHandVar = rightHandValue ;

First, we place the name of the variable we want to assign a value to. If a variable does not yet have a specified value, then this process is called initializing. A variables initial value is the value it is first set to. You can also initialize a variable at the same time it is created like, private int intVar = 4; If you don’t initialize a variable it will either be set to a default value like zero, or will be set to null, which means no value.

Next we have the equal operator. This operator gets the preceding variable ready to be assigned a value.

Finally, we have the new value we want the variable to be set to.
You can also assign a variable to equal the value of another variable. This could look like, intVar = intVar2;

Modifying Variable Values
After you have assigned a value to a variable, you will probably want to be able to modify that value. For numbers, there are some of the normal math operators that can be used. These operators can be used as such:

int1 += 10;
int1 -= 10;
int1 *= 10;
int1 /= 10;

These operators change the values of variables. The += means the same thing as, int1 = int1 + 10; You can also modify the right hand value by doing something like, int1 += 10 * 3;

No comments: