Tuesday, June 22, 2010

Programming Introduction lesson 1 - Your First program in C#

My First Program In C#


This is code automaticly created by IDE,that saves us lot of time because with can start writing our code right away!
/*
* Created by SharpDevelop.
* User: User
* Date: 30.6.2010
* Time: 6:57
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace My_First_program_in_C_
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
}
}
Variables - Programming Introduction

In programming variables are empty space in our program that we reserve so something can be writen in them and changed when you like.So how can you get your program to remember something?Use variables to store whatever was your or users input and when it's stored you can do with them what ever you want,read them,change them...etc.

So we had for exemple String,strings are variables in which text can be writen.There are two main types of variables in C# string and int,int marks Integer so they are variables in which numbers can be writen.

So for exemple in our first program we had :

string Name = textBox1.Text;So we declared string called Name,and we wanted that when user input their name in our textbox object we store that information for later use in program.So we put textBox1 to declare to compiler from which of our object we would like to use text,when you press dot SharpDevelop will offer you many functions and parametars,parametars can be changed dinamically,like for example color of our textbox,width,height etc.So we want to read text that user put in our textBox1 when he presses button which tells to us he is over writting.Cleaner code lines would be:

string Name;
string
Name = textBox1.Text;

So when we declare the string,it's empty after that we write whatever we want to it,in this case textBox1.Text.

So when we store variable that declares user's name we want to use it to display it somwhow in our program:

MessageBox.Show("Hello " + Name + ",welcome to first lesson of Programming Introduction examples." +
"Powerd By programmingintroduction.blogspot.com",
"Example messagebox Title",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);


Syntax code for function MessageBox.Show is MessageBox.Show(Message you want to display in body of messageBox,Title of the MessageBox,Buttons we want to have on out messageBox,Icon in the MessageBox).

When we want to tell our IDE we want to display our own text that is static,we use quoutes(""),So that compiler would know we would like to display it as is.We use quotes so Compiler would know when we would like to display static text,and we don't use it when we want to display contents of string,so if I write :

MessageBox.Show("Hello " + "Name" + ",welcome to first lesson of Programming Introduction examples." +
"Powerd By programmingintroduction.blogspot.com",
"Example messagebox Title",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);

The Output would be the same whatever the user input,because compiler would assume that we wanted to writte "Hello Name,welcome.." but when we remove the quotes from name,compiler knows that we are trying to display string.ALL TEXT THAT IS FIXED IN C# NEEDS TO BE ENCLOSED BY QUOTES LIKE THIS : "This is some text";

MessageBox

Syntax in C#

Function(parametar1,parametar2,parametar3.....);

Every line in C# has to be closed with ";" except statements,but I will explain that when we come to that!You can see that parametars are separated with ","!In case of MessageBox.Show parametars are MessageBox.Show(bodytext,title,buttons,icon); and they are different for every function!

We can change the icon as well by changing parametar4 in our MessageBox.Show,If you would like try changing our code a little bit a put instead of MessageBoxIcon.Asterisk,MessageBoxIcon and then put dot SharpDevelop should offer you available icons :

MessageBoxIcon.Asterisk
MessageBoxIcon.Error
MessageBoxIcon.Exclamation
MessageBoxIcon.Hand
MessageBoxIcon.Information
MessageBoxIcon.None
MessageBoxIcon.Question
MessageBoxIcon.Stop
MessageBoxIcon.Warning

So try to change this parametar and recompile program by pressing green arrow as shown on pictures and see how you MessageBox looks change!

Monday, June 21, 2010

Choosing the right IDE

So after we installed framework that we need,it is time to move on and to choose integrated development environment (IDE) you like.IDEs are applications where you will code and design your own programs.There are some free tools on the Internet for writing C# programs where visual design isn't available so you have to write everything in code.That is not practical for designing,this tools are only useful if you are experienced or just want to get the job done without hassle of visual design.There are two good C# IDEs available on the internet,one is open source and other is commercial :
  • Microsoft Visual Studio - Commercial
  • Sharp Develop - Open Source tool - Freeware
There is not really any difference in these two IDEs,but you will get some complicated things done easier with Visual Studio,but in reality Sharp develop is great tool and can seriously compete with Visual studio.I will use Sharp Develop 4.0,altought I have Visual Studio 2010 Ultimate Beta 2 because I'm a member of MSDN Academic Alliance.

Sharp Develop

Microsoft Visual Studio

Friday, June 18, 2010

Before we choose IDE


Before we move to choosing IDE you will have to download and install:
In order for user to run program that you have compiled with Microsoft .NET framework 4 on his system he will need to have Microsoft .NET framework 4 installed as well,this is only limitation to C# but you can later include it when you create installation files and put automatic download of the missing component.

You can choose to compile your programs with older version because users will more likely to have it installed already.But if you want to get full functionality or write for newer OS (Operating System) you should get Microsoft .NET framework 4 and besides you can include automatic download of a it in installation process of program you will make.

.NET enables C# language to stay simple and small,in there complicated functions get shorter and more simple but stay as powerfull.Note that you can run file compiled with framework 3 if you have framework 4 installed but that doesn't go reverse.

I will write my examples with Microsoft .NET framework 4

Openning of Programming Introduction Blog

Hello everyone I would like to give you basic programming introduction as well as some advanced stuff over the time,look on it like a course of programming that you can follow without any pressure.If you are interested in programming and have strong will you will learn it very quickly.So for introduction I will tell you little about myself.I have about 6 years of experince in programming in different programming languages such as : C/C++,assembler,Visual Basic,C#,Java,Jscript,php.But my main passion is C# (C-sharp) programming language so this introduction guide and blog will be based on it.I will start with GUI programming from beginning because I don't like that old console applications.I will write posts as often as I can and provide detailed explanations with all the examples I post.If you miss any part you can easly go to posts archive and check it out,in my next post I will talk about integrated development environment (IDE) where you will do all the magic of writing your programs.That's it for now,remember to follow blog for new posts and try searching Google for C#.

Vote