This topic is being posted on massive request from new programmers.
Well I want to say that Namespace is not an Object Oriented Concept, but I will like to explain it.This concept is in different Object Oriented languages and is used frequently ,through a real world example I want to explain so that some new programmers get to know what this ' thing' is , but for sure its not an Object Oriented Concept.
Namespace
A namespace is hidden or an abstract container for holding logical group of unique identifers.Now this identifier can also be defined in other multiple namespaces but the meanging of that identifier can change their.
For Example
Jhon studies in a school X and Asif studies in school Y.. Jhon's student ID is 7121 and the sudent ID of Asif is 7121.See both of the ID's of Jhon and Asif are same but also both have the different meaning one is for Jhon and one is for Asif.
Some C# Examples
C# and Namespace
Program.No.1
using System;
namespace ABC
{
class NamespaceABC
{
public static void main()
{
Console.WriteLine("In this Program a namesspace ' System ' is used.. ");
}
}
}
Program.No.2
using System;
namespace NestedNamespce
{
namespace practice
class NesNamSpace
{
public static void Main()
{
Console.WriteLine("This is the new C# Station Tutorial Namespace.");
}
}
}
}
Program.No.3
using System;
namespace ABC
{
namespace practice
{
class myExample1
{
public static void myPrint1()
{
Console.WriteLine("Example Number 1 calling another namespace memeber ");
}
}
}
// Program start class
class NamespaceCalling
public static void Main()
{
practice.myExample1.myPrint1();
practice.myExample2.myPrint2();
}
}
ABC.practice
{
class myExample2
{
public static void myPrint2()
{
Console.WriteLine("Example Number 2 . calling another namespace memeber");
}
}
}
_______________________________________________________
Also if you do not want to use the namespace using System or if you have forgot to write on the top , then you have to write the Console.WriteLine("");
statement as,
System.Console.WriteLine("");
Monday, October 27, 2008
Saturday, October 25, 2008
OOP
OOP is Object Oriented Programming.
Why OOP ?Well it is used for reusability , and to decrease code maintainance and easily deal with the complexity of programs.
What is OOP ?
Well the programming in which the programmers deal with objects.
Friday, October 24, 2008
Class , Object and C#
Note : Sorry for being late , just got to busy . You will INSHALLAH see my posts everyday.
Class is used as a blueprint (design) to create objects. Class is a building block which contains some functions and properties that describe some set of objects.
For Example:A class of a person,a person has some properties that he/she has hair, eyes, nose and so on. Functions include that a person can walk, smell, observe.Now here class is a blueprint, through this class or blueprint we can make more persons with different attributes and functions by creating an object.When the object of a person is created you can give different attributes to him , for example
OBJ1: He has brown eyes, and 5’8’’ inch height.
OBJ2: He has blue eyes, with a very small height. They both are an object of class person with different propertoes.
The following program will help you alot.The red label are namespaces
Program :
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
int h;
int w;
int a;
public void observe(int height, int weight)
{
if (height >= (weight / 4))
Console.WriteLine("Your very good in observing. ");
else
Console.WriteLine("Your very poor in observing. ");
}
public void taste(int height, int weight, int age)
{
if (height >= (weight / 4) && weight == (age * 2))
Console.WriteLine("You know how to taste things.");
else
Console.WriteLine("You are very poor to taste things.");
}
static void Main(string[] args)
{
Program obj1 = new Program();
Program obj2 = new Program();
Console.WriteLine("TWO DIFFERENT PERSONS CAN HAVE DIFFERENT PROPERTIES ");
Console.WriteLine("-------------------------------------------------"); Console.WriteLine("For PERSON NO 1 Does person 1 know hot to observe things ");
Console.WriteLine("Enter your height ");
obj1.h = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your weight to know how u observe");
obj1.w = Convert.ToInt32(Console.ReadLine());
obj1.observe(obj1.h, obj1.w);
Console.WriteLine("-------------------------------------------------"); Console.WriteLine("For PERSON NO 2 Does person 2 knows hot to taste things ?");
Console.WriteLine("Enter your height ");
obj2.h = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your weight to know how u taste");
obj2.w = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your age");
obj2.a = Convert.ToInt32(Console.ReadLine());
obj2.taste(obj2.h, obj2.w, obj2.a);
Console.ReadLine();
}
}}
Class is used as a blueprint (design) to create objects. Class is a building block which contains some functions and properties that describe some set of objects.
For Example:A class of a person,a person has some properties that he/she has hair, eyes, nose and so on. Functions include that a person can walk, smell, observe.Now here class is a blueprint, through this class or blueprint we can make more persons with different attributes and functions by creating an object.When the object of a person is created you can give different attributes to him , for example
OBJ1: He has brown eyes, and 5’8’’ inch height.
OBJ2: He has blue eyes, with a very small height. They both are an object of class person with different propertoes.
The following program will help you alot.The red label are namespaces
Program :
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
int h;
int w;
int a;
public void observe(int height, int weight)
{
if (height >= (weight / 4))
Console.WriteLine("Your very good in observing. ");
else
Console.WriteLine("Your very poor in observing. ");
}
public void taste(int height, int weight, int age)
{
if (height >= (weight / 4) && weight == (age * 2))
Console.WriteLine("You know how to taste things.");
else
Console.WriteLine("You are very poor to taste things.");
}
static void Main(string[] args)
{
Program obj1 = new Program();
Program obj2 = new Program();
Console.WriteLine("TWO DIFFERENT PERSONS CAN HAVE DIFFERENT PROPERTIES ");
Console.WriteLine("-------------------------------------------------"); Console.WriteLine("For PERSON NO 1 Does person 1 know hot to observe things ");
Console.WriteLine("Enter your height ");
obj1.h = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your weight to know how u observe");
obj1.w = Convert.ToInt32(Console.ReadLine());
obj1.observe(obj1.h, obj1.w);
Console.WriteLine("-------------------------------------------------"); Console.WriteLine("For PERSON NO 2 Does person 2 knows hot to taste things ?");
Console.WriteLine("Enter your height ");
obj2.h = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your weight to know how u taste");
obj2.w = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your age");
obj2.a = Convert.ToInt32(Console.ReadLine());
obj2.taste(obj2.h, obj2.w, obj2.a);
Console.ReadLine();
}
}}
Friday, October 17, 2008
What is an object ?
Real World Object : Any 'thing' that can perform a set of activities is known as an object.
Real World objects share two things :
1.state (properties)
2.behaviours(functions)
Lets see a real wolrd example : Your car its an object because it can perform set of activities, your car has certain properties and performs certain functions.Your dog,television and bicycle are also objects with some state (properties) and behaviour(functions).
Object in Programming terms:
An object is an instance of a class, here comes a question what is a class.
Well class is simply a presentation of object.
You we will see more topics later this is just a start.
Real World objects share two things :
1.state (properties)
2.behaviours(functions)
Lets see a real wolrd example : Your car its an object because it can perform set of activities, your car has certain properties and performs certain functions.Your dog,television and bicycle are also objects with some state (properties) and behaviour(functions).
Object in Programming terms:
An object is an instance of a class, here comes a question what is a class.
Well class is simply a presentation of object.
You we will see more topics later this is just a start.
Subscribe to:
Posts (Atom)