Monday, February 21, 2011

Variables

Variables are words that can hold data. When you have to show the same "Hello World!" on your screen ten times, then it's easier to put that data in a word. This saves you from typing "Hello World!" all the time. Sometimes you will make the variable name longer than the text itself, but that doesn't matter. Visual Studio has a list of all the variables you made and it will give you the option to just type the first few letters and then you can press "Enter" on your keyboard. This will autocomplete your word. It's like when you type something in Google and it will autocomplete what you're searching for, that is if what you're searching for is in Google's list.


Let's start by opening your old "Hello World!" project or make a new one with the exact same code. Replace the text "Hello World!" with a variable.


Start with declaring the variable "helloWorld" by typing "string helloWorld = "Hello World!";" above "Console.Write("Hello World!");" and replace "Console.Write("Hello World!");" with "Console.Write(helloWorld);". If you run your application by pressing F5 on your keyboard now it should show the exact same thing as when you started it the last time.


String is a special data type in C# namely a basic data type. String tells your variable with the name "helloWorld" that it will hold multiple letters and/or signs or in our case "Hello World!". In C# there are in total 15 basic data types and are shown in blue in Visual Studio:
  • bool : true or false
  • sbyte: -128 to 127.
  • byte : 0 to 255
  • char : One letter or sign
  • short : -32,768 to 32,767
  • ushort : 0 to 65,535
  • int : -2,147,483,648 to 2,147,483,647
  • uint ; : 0 to 4,294,967,295
  • long : -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • ulong : 0 to 18,446,744,073,709,551,615
  • decimal : ±1.0 × 10^−28 to ±7.9 × 10^28 - numbers with comma
  • float; : ±1.5 × 10^−45 to ±3.4 × 10^38 - numbers with a comma
  • double : ±5.0 × 10^−324 to ±1.7 × 10^308 - numbers with comma
  • string : Multiple letters and/or signs
  • object : This can be anything. It's data without a meaning.
You'll probably think "Why not just always use string?". That's because you'll want to see the difference between data, so you can expect what kind of data it will be. When you will use libraries of the .NET Framework you won't be able to see the code behind it, but you will know what kind of data it will give you thanks to the basic data types. Memorise that "6" is a string (like "Hello World!") and 6 without " signs around it is a number like int, short, long and etc. The same counts for booleans (bool for short): "true" is a string and true without " signs around it is a boolean.

You will probably have to practice a lot in order to memorise all the basic data types and you should, because they're really important. The most used basic data types are: string, object, int and bool, so you should start with those.


Back to the coding part. You can show numbers on your screen by using int. Replace "string helloWorld = "Hello World!";" with "int randomNumber = 25;" and replace "Console.Write(helloWorld);" with "Console.Write(randomNumber);". Run your application and you should see the number "25" on your screen.


You can do the same with booleans (bool for short). Replace "int randomNumber = 25;" with "bool randomBoolean = true;" and replace "Console.Write(randomNumber);" with "Console.Write(randomBoolean);". Run your application and you should see "True" on your screen.


Until now you've only used variables with basic data types, but there are also normal data types which you can add yourself or are available in the .NET Framework. Replace "bool randomBoolean = true;" with "Random myRandomVariable = new Random();" and replace "Console.Write(randomBoolean);" with "Console.Write(myRandomVariable.Next());".


Random is a class in the .NET Framework. "new Random()" returns data with the data type Random (convenient huh?). Your "myRandomVariable" variable now has the information to call the "Next" method of the "Random" class. The "Next" method returns a random number with the basic data type int (point with your cursor on "Next"). You should see a random number on your screen when you start your application.

Note: You should not take it easy on naming your variables. You should give variables a correct description based on the data it will hold in order for you and others to easily understand your (large) projects.

Source Code Variables Project

>> Basic Calculations

2 comments: