Subscribe

Variables in C#

Posted by Administrator on Saturday, July 2, 2011

Using variables is one of the key concept when programming.

What are variables?
          Variables are used to store values,data within our programme. We need to store numbers, characters, strings, dates and many more things withing our programme. To store them, we have to declare variables.
Variable has a datatype,name and the value.

  • Datatype - The type of the variable ( number, character, string, date or etc..)
  • Name - The identifier of the variable (Note that variable naming follows Camel casing)
  • Value - The value assigned to the variable

Syntax to declare variables: 
<datatype> <variableName>;

Assign values to variables: (initialization)
<variableName> = <value>;

 We can  declare and the initialize the variable at the same time

<datatype> <variableName> = <value>;

//declare and initialize when need
string name;
name = "my name";

//declaration + initialization 
string name = "my name";

C# supports many inbuilt data types or we can create custom data types as well.
Here are the C# in-built data types.

Data Type
Range
byte0 .. 255
sbyte-128 .. 127
short-32,768 .. 32,767
ushort0 .. 65,535
int-2,147,483,648 .. 2,147,483,647
uint0 .. 4,294,967,295
long-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
ulong0 .. 18,446,744,073,709,551,615
float-3.402823e38 .. 3.402823e38
double-1.79769313486232e308 .. 1.79769313486232e308
decimal-79228162514264337593543950335 .. 79228162514264337593543950335
charA Unicode character.
stringA string of Unicode characters.
boolTrue or False.
objectAn object.


Exercise: Write a programme that accepts your name, age and print them on the console.

Code:
using System;


class Program
{
    static void Main(string[] args)
    {
        //Declare a variable to store name 
        string name;
        //Declare a variable to store age
        int age;

        //Getting name from the user
        Console.Write("Enter your name? ");
        name = Console.ReadLine();

        //Getting age from the user
        Console.Write("Enter your age? ");
        age = Convert.ToInt32(Console.ReadLine());

        //Print name and age
        Console.WriteLine("My name is " + name);
        Console.WriteLine("I am " + age + " years old.");

    }
}

Save the file as Program.cs any where you want. You can compile c# using the command line version.

csc Program.cs

And then run the new program by entering

Program

Output:




Declaring constants

Constant has a value that does not change. They are declared as a field. C# uses the const keyword before the type of the field.And the most important thing is constants must be initialized as they are declared.

/* declaring a constant
* should initialize as declared
* value does not change*/
const double PI = 3.14159;

/* Error you cannot change the 
* value of a constant*/
PI = 3.14;


An Example:

using System;


class Program
{
    static void Main(string[] args)
    {
        const double PI = 3.14159;
        double radius = 10;
        double areaOfTheCircle = PI * radius * radius;

        Console.WriteLine("Area of the circle is " + areaOfTheCircle);
    }
}

Output:

Your First C# Application

Posted by Administrator on Thursday, June 30, 2011

We are now going to create a simple application as your first C# programme. Before building this application you should already install Microsoft .Net Framework and Microsoft Visual Studio 2008 as we discussed earlier.

And the other thing is you should have a basic idea of


So Let's start our first C# application. Here what we do is, We create a simple user interface with a button. When the button is clicked, the message "Hello World!" is raised.

Step 1: Start Visual Studio
Open the Microsoft Visual Studio 2008. And It is look like this when you start visual studio first time.


Step 2: Create a new project
Then Using visual studio, we should create a new project.
Go to File -> New Project...  or Press Ctrl +Shift + N
Then New Project Dialog Appears.



In the New Project Window, Select Visual C# as Project type and Windows Forms Applications as the template. Give Name and Location to your project and finally click OK button to create our first C# project.

Step 3: Design the user interface.
When the project is created, You will see the designer view of your interface as follows.

Form Designer View

In this designer view of the form (Form1.cs [Design]), you can design the user interface of the single form. To do that, We use the 'Toolbox' which contains the items that you can add to your form. Toolbox is placed on the left side of your visual studio. If it is not visible Go to View -> Toolbox to show the Toolbox.   




Toolbox


It contains Lables, Buttons, Check Boxes, Combo Boxes and etc.. which can be used to design your interface. To add elements from the Toolbox to your form double click the item or drag the item to your form.
Now add a Button Control to your form by simple dragging a Button control into the form designer view. Finally it looks like below.



Now our form contains two elements. Those are form and the button control.  These elements have properties such as name, text, background color, fore color, etc.... To see properties for a control, select the control and all the properties are displayed in the Properties Window which appears on the right side of your visual studio. If it is not visible, Go to View ->  Properties Window. 

Select the button and view properties as follows.


Now set the text to 'Show' for the button. Then select the form set text to 'Hello World' from the Properties Window. Then the form should look like below.


Step 4: Writing the code
When we click the button, a message with 'Hello World!' should be shown. To do that we have to go to the code view of the form. Double click the button and visual studio automatically switch to the code view (Form1.cs). In the button1_Click event (this will execute when we click the button), write the code to raise the message box with  'Hello World!'.

Code to show the message box:

MessageBox.Show("Hello World!");


Now the form1.cs looks like below.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelloWorld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World!");
        }
    }
}

Step 5: Compile the code
To compile the code Go to Build -> Build HelloWorld

Step 6: Running the application
To run/execute the programme  press F5.


Running Application.

So its over. Your first C# application. Try more tutorials to become a C# expert. 

Microsoft Visual Studio 2008

Posted by Administrator on Saturday, June 25, 2011

        Microsoft Visual Studio (VS) is an Integrated Development Environment (IDE) from Microsoft Cooperation. It can be used to develop various kinds of applications using Microsoft .Net Framework for the platforms supported by Microsoft. Those platforms are Microsoft Windows, Windows Mobile, and Windows CE. Using VS, we can develop console and graphical user interface (GUI) applications. Console applications are the applications running on windows terminal. And GUI applications are the applications which have rich user interface such as Windows Forms applications, web sites, web applications and web services.


Microsoft Visual Studio 2008


        Visual Studio makes the development process easier and faster. It includes a code editor on which developers can write source codes of the applications. It supports intelliSense as well as code refactoring. It has an inbuilt debugger. Using that, developers can debug .Net applications. Visual Studio has other inbuilt tools. It includes a forms designer for building GUI applications, web designer, class designer and database schema designer. So that developers can build rich applications without doing huge amount of coding. We can create plug-ins for the Visual Studio to enhance the functionality of the tool. It supports for source-control systems (like Subversion and Visual SourceSafe) so that many developers can work in a same project.
Here are the most important and useful Visual Studio 2008 features.

IntelliSense
Code Snippets
Revision Marks
Designer Improvements
XML Support
Refactoring
Partial Classes
Debugging
Other IDE Improvements

Because of those features Visual Studio 2008 becomes one of the main Integrated Development Environment for developing .Net applications.

Microsoft .Net Framework

Posted by Administrator on

       Microsoft .Net framework is one of the world's greatest development framework developed by Microsoft. And this is a software framework for Microsoft Windows operating systems.Using .Net framework, developers can build standalone desktop applications, web applications and mobile applications. It provides lot's of class libraries to work with user interfaces, data access, database connectivity, cryptography, numerical algorithms and network communications.




       Microsoft .Net Framework supports for many programming languages therefore developers can use any language to develop their applications as they wish. Those languages are Visual C#, Visual Basic, Visual C++, Visual J#, etc… Because of that reason Microsoft .Net Framework becomes more and more popular among the software developers. Using Microsoft .Net Framework, developers can build applications very easily and fastly.

Let’s look how the compilation is done within the Microsoft .Net Framework. First developer builds the application using any .Net language like C# and developer compiles that source code using C# compiler to intermediate language (IL). If developer builds the application using VB language, he/she needs VB language compiler to compile source code to intermediate language (IL). After initial compilation, we get a complied source code with intermediate language. It is called .Net assembly. .Net assembly can be either .dll file or .exe file. By looking at the assembly file, we can’t guess the programming language which is used to develop the application because the assembly file is independent from the .Net language. Then the second compilation is done. The Common Language Runtime (CLR) which provides runtime for the .Net applications compiles the assembly file into the machine code. Machine code only contains a stream of 0’s and 1’s. Machine code is the code that the computers can understand because it only contains 0’s and 1’s. Then the machine code is executed by the computer. It means the computer executes the application. And finally user can work with the application. 


The following figure demonstrates how the .Net compilation process is done.

.Net Framework Compilation

That is some information about Microsoft .Net Framework and its compilation process. You can read more on it from Wikipedia. You are going to learn C# and now you know why we installed .net Framework to develop C# applications. 





Installing Visual Studio 2008

Posted by Administrator on

This Article will explain how to install Microsoft Visual Studio 2008 and If you like you can use the latest version Microsoft Visual Studio 2010.

To install Visual Studio 2008, You need the software. But Microsoft Visual Studio 2008 is a commercial product. But they provide the free version of it. It is called Microsoft Visual Studio 2008 Express Edition. You can download the latest version here.

Or you can find any commercial version of  Microsoft Visual Studio 2008. And the following video will demonstrate you how to install it on your computer.  



Now you have successfully installed Microsoft Visual Studio 2008. The development environment is now set.
Then we have to start learning C#. Go through the tutorials and become a C# hero.

Subscribe to: Posts (Atom)