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!

Vote