Sunday, 10 February 2013

Constructors,Members Object,object initializer



Demo on Constructors

using System;
namespace FreshersLike.Constructors
{
    class Test
    {
        public int ns;             //Non-Static variable
        public static int s;    //Static variable

        static Test()          //Static Constructor
        {
            s = 100;

            //ns = 100; Error : Static constructors cant initialize Non-Static variables
        }

        public Test()       //Non-Static Constructor
        {
            ns = 1000;
            s = 1000;
        }
    }

    class ConstructorsDemo
    {
        public static void Main()
        {
            Console.WriteLine("Value of S before object creation : {0} ", Test.s);

            Test t = new Test();

            Console.WriteLine("Values of NS and S : {0} , {1} " , t.ns , Test.s );
        }
    }
}

.........................................................................................................................................


Demo on Class , Members and Object

using System;

namespace FreshersLike
{
    class Test
    {
        public int ns;            //Non-Static Member
        public static int s;    //Static Member
    }

    class MembersDemo
    {
        public static  void Main()
        {

            Test t1 = new Test();
            Test t2;

            t2 = new Test();
            t1.ns = 100;

            t2.ns = 1000;
            // t1.s = 500; Error

            Test.s = 500;
            Console.WriteLine("{0} , {1} , {2} ", t1.ns , t2.ns , Test.s);
        }
    }
}

........................................................................................................................................


Demo on Object initializer  of C# 3.0

using System;
namespace FreshersLike
{
    class Department
    {
        public int Deptno;

        public string Dname, Loc;

        public static int TotalDepartments;

        static Department()
        {
            TotalDepartments = 0;
        }

        public Department()
        {
            TotalDepartments += 1;
        }
    }

    class ObjectIntializerDemo
    {
        public static void Main()
        {
            Department d1 = new Department()
            {
                Deptno = 10,
                Dname = "SALES",
                Loc = "HYD"
            };

            Department d2 = new Department()
            {
                Deptno = 20,
                Loc = "Bang"
            };

            Department d3 = new Department() { Deptno = 30 };

            Console.WriteLine("Total Departments : {0} " , Department.TotalDepartments );

            Console.WriteLine("Department # : {0} \nDepartment Name : {1} \nLocation : {2} ", d1.Deptno , d1.Dname , d1.Loc );

            Console.WriteLine("Department # : {0} \nDepartment Name : {1} \nLocation : {2} ", d2.Deptno, d2.Dname, d2.Loc);

            Console.WriteLine("Department # : {0} \nDepartment Name : {1} \nLocation : {2} ", d3.Deptno, d3.Dname, d3.Loc);

        }
    }
}

.....................................................................................................................................



Demo on Optional Parameter & Named Parameter Passing.

using System;

namespace FreshersLike
{
    class Employee
    {
        private int Empno;

        private string Ename , Job;

        private decimal Salary, Commission;

        public void SetEmployee(int eno, string ename, decimal comm = 0, decimal salary = 10000, string job = "SALESMAN")
        {
            Empno = eno;

            Ename = ename;

            Job = job;
            Salary = salary;
            Commission = comm;
        }

        public void ShowEmployee()
        {
            Console.WriteLine("Employee # : {0} \nEmployee Name : {1} \nSalary : Rs. {2} \nCommission : Rs. {3} \nJob : {4} ", Empno , Ename , Salary , Commission , Job );
        }
    }

    class OptionalParameterDemo
    {
        public static void Main()
        {
            Employee emp = new Employee();

            //emp.SetEmployee(101, "Ramesh", salary: 50000, job: "Manager");

            emp.SetEmployee(101, "Kumar", 1000);

            emp.ShowEmployee();

        }
    }
}

Posted on 23:22 | Categories:

Friday, 8 February 2013

Chat box demo

How to create a chat by using .NET

Step 1:

>Create a New empty Website

>Right click on Application Add new Item

> Select File Global Application class and name it as global.asax

> Write Below code in global.asax

Global.asax

// Application_Start( write code in)

Application.Lock();

Application["tusers"]=0;                                           Note: tusers= Total Users

Application["ousers"]=0;                                                      ousers=Online users

Application["msgs"]="";

Application.Unlock();

// Session_Start(write code in)

int tusers,ousers;

Application.Lock();

tusers=(int)Application["tusers"]+1;              // Unboxing

ousers=(int)Application["ousers"]+1;

Application["tusers"]=tusers;

Application["ousers']=ousers;

Application.Unlock();

// Session_End(write code in)

int ousers;

Application.Lock();

ousers=(int)Application["ousers"]-1;

Application["ousers"]=ousers;

Application.Unlock();

Step 2:

Add a webform 

>Name it as Default.aspx

Default.aspx [Design]

> Design like below diagram

> Use table (4 rows , 1 column)

(To see complete image click on it)
                                                          

// Page_Load (write code in)

Application.Lock();

lblTotalUsers.Text=Application["tusers"].Tostring();

lblOnlineUsers.Text=Application["ousers"].ToString();

lblMsg.Text=Application["msgs"].ToString();

Application.Unlock();

// Logout_buttonclick(write code in)

Session.Abandon();

// Send_buttonclick(write code in)

string uname,msg,msgs;

uname=txtName.Text;

if(uname.Lenght>0)

msg.string.Format("<font color =green> {0}:</font> {1}",uname,txtMsg.Text);

else

msg=string.Format("<font color =Red> Guest:</font> {0}",txtMsg.Text);

Application.Lock();

msgs=Application["msgs"].ToString() + "<br>" + msg;

Application["msgs"] = msgs;

Application.Unlock();

lblmsg.Text=msgs;




                                                                    ALL THE BEST






Posted on 01:44 | Categories:

Thursday, 7 February 2013

Sorting techniques in C




/* Write a program of Binary Tree Sort. */

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct Node
{
int info;
struct Node *left;
struct Node *right;
struct Node *father;
};

typedef struct Node *Nodeptr;

Nodeptr getnode()
{
return( ( Nodeptr ) malloc( sizeof ( struct Node ) ) );
}

Nodeptr makenode( int x )
{
Nodeptr temp;

temp = getnode();
temp->info = x;
temp->left = temp->right = temp->father = NULL;

return( temp );
}

Nodeptr buildtree( Nodeptr root, int x )
{
if ( root == NULL )
root = makenode( x );

else if( x <= root->info )
{
root->left = buildtree( root->left, x );
root->left->father = root;
}

else
{
root->right = buildtree( root->right, x );
root->right->father = root;
}

return( root );
}

void intraverse( Nodeptr root, int *a )
{
static int i = 0;

if( root == NULL )
return;

intraverse( root->left, a );

*( a + i++ ) = root->info;

intraverse( root->right, a );
}

void binarytreesort( int *a, int n )
{
Nodeptr root;
int i;

root = NULL;

for( i = 0; i < n; i++ )
{
root = buildtree( root, *( a + i ) );
}

intraverse(root, a );
}

void main()
{

int i, n, x;
int *item;

clrscr();

printf("Enter total number of items: ");
scanf("%d",&n );

item = ( int * ) malloc( n * sizeof( int ) );
printf("Enter items: ");
for( i = 0; i < n; i++ )
{
scanf("%d", ( item + i ) );
}

binarytreesort( item, n );

printf("\nThe sorted list => ");
for( i = 0; i < n; i++ )
printf("%d ", *(item+i) );

getch();
}

...........................................................................................................................................


/* Write a program of Bubble Sort. */

#include <stdio.h>
#include <conio.h>

void bubblesort( int item[], int n )
{
int flag, pass, count;

for( pass = 0; pass < n - 1; pass++ )
{
flag = 0;

for( count = 0; count < n - 1 - pass; count ++ )
if( item[count] > item[count+1] )
{
item[count] = item[count] + item[count+1];
item[count+1] = item[count] - item[count+1];
item[count] = item[count] - item[count+1];
flag = 1;
}

if( flag == 0 )
break;
}
}

void main()
{
int *item, n, i;

clrscr();
printf("Enter total number of items: ");
scanf("%d",&n);

item = ( int * ) malloc( n * sizeof( int ) );

printf("Enter the items: ");
for( i = 0; i < n; i++ )
scanf("%d", (item+i) );

bubblesort( item, n );

printf("\nThe sorted list => ");
for( i = 0; i < n; i++ )
printf("%d ", *(item+i) );

getch();
}
..................................................................................................................................................

/* Write a program of Quick Sort. */

#include <stdio.h>
#include <conio.h>

int partition( int item[], int lb, int ub )
{
int down, up, temp, x;

down = lb;
up = ub;
x = item[lb];

while( down < up )
{
while( item[down] <= x && down < ub )
down++;

while( item[up] > x )
up--;

if( down < up )
{
temp = item[down];
item[down] = item[up];
item[up] =temp;
}
}

item[lb] = item[up];
item[up] = x;
return( up );
}


void quicksort( int item[], int lb, int ub )
{
int p;

if( lb > ub )
return;

p = partition( item, lb, ub );

quicksort( item, lb, p - 1 );

quicksort( item, p + 1, ub );
}



void main()
{
int *item, n, i;

clrscr();
printf("Enter total number of items: ");
scanf("%d",&n);

item = ( int * ) malloc( n * sizeof( int ) );

printf("Enter the items: ");
for( i = 0; i < n; i++ )
scanf("%d", (item+i) );

quicksort( item, 0, n-1 );

printf("\nThe sorted list => ");
for( i = 0; i < n; i++ )
printf("%d ", *(item+i) );

getch();
}
.................................................................................................................................................

/* Write a program of Selection Sort. */

#include <stdio.h>
#include <conio.h>

void selectionsort( int item[], int n )
{
int large, index, pass, count;

for( pass = n - 1; pass > 0; pass-- )
{
large = item[0];
index = 0;

for( count = 1; count <= pass; count++ )
if( item[count] > large )
{
large = item[count];
index = count;
}

item[index] = item[pass];
item[pass] = large;
}
}

void main()
{
int *item, n, i;

clrscr();
printf("Enter total number of items: ");
scanf("%d",&n);

item = ( int * ) malloc( n * sizeof( int ) );

printf("Enter the items: ");
for( i = 0; i < n; i++ )
scanf("%d", (item+i) );

selectionsort( item, n );

printf("\nThe sorted list => ");
for( i = 0; i < n; i++ )
printf("%d ", *(item+i) );

getch();
}





Posted on 21:50 | Categories:

Complete Methods Implementation



//Demo on Complete Methods Implementation

using System;

namespace FreshersLike
{

    class Institute
    {

        public string CourseName;

        public decimal Fees, Discount;

        public Institute( string cname , decimal fees )
        {

            CourseName = cname;

            Fees = fees;

            Discount = 0;
        }

        public void ShowCourse()
        {

            Console.WriteLine("Course Name : {0} \nFees : Rs. {1} \nDiscount : Rs. {2} ", CourseName,Fees , Discount);

        }

        public void GetDiscount(decimal Percentage, out decimal Discount)
        {

            Discount = Percentage * 10;
        }

        public void SetCourseFees(ref decimal Fees, decimal discount)
        {

            Fees -= discount;

        }
    }

    class CompleteMethodImplementation
    {

        public static void Main()
        {

            Institute freshers_net = new Institute(".NET Package", 4900);

            Console.WriteLine("Freshers Like Institute Details : ");

            freshers_net .ShowCourse();

            decimal percentage;

            Console.Write("Enter your percentage : ");

            percentage = decimal.Parse(Console.ReadLine());

            freshers_net.GetDiscount(percentage, out freshers_net.Discount);

            Console.WriteLine("Congrats , You have got a discount of Rs. {0} ", freshers_net.Discount );

            freshers_net.SetCourseFees(ref freshers_net.Fees, freshers_net.Discount);

            Console.WriteLine("Final Course Fees will be Rs. {0} ", freshers_net.Fees );

        }
    }
}



















Posted on 21:37 | Categories:

Wednesday, 6 February 2013

call by value Call by Reference example




//Demo on Call By Reference

using System;

namespace FreshersLike
{

    class CBRClass

    {

        public void Swap(ref int no1, ref int no2)
        {

            int temp;

            temp = no2;

            no2 = no1;

            no1 = temp;
        }
    }

    class CallByReferenceDemo
    {

        public static void Main()
        {

            CBRClass obj = new CBRClass();

            int no1, no2;

            Console.WriteLine("Enter two numbers : ");

            no1 = int.Parse(Console.ReadLine());

            no2 = int.Parse(Console.ReadLine());

            Console.WriteLine("Before Swapping Values are : {0} , {1} " , no1 , no2 );

            // obj.Swap(no1, no2); Error

            obj.Swap(ref no1, ref no2);

            Console.WriteLine("After Swapping Values are : {0} , {1} " , no1 , no2 );
        }
    }
}




//Demo on Call By Value

using System;

namespace FreshersLike
{

    class Name
    {

        private string FirstName, LastName;

        public void SetName(string fname, string lname)
        {

            FirstName = fname;

            LastName = lname;
        }

        public string GetName()
        {

            string fullName;

            fullName = string.Format("{0} {1}", FirstName, LastName);

            return fullName;
        }
    }

    class CallByValueDemo
    {

        public static void Main()
        {

            Name name = new Name();

            name.SetName( lname: "srinivas" , fname: "sekhar" );

            Console.WriteLine("Name : {0} " , name.GetName());
        }
    }
}











Posted on 21:26 | Categories:

Monday, 4 February 2013

Boxing and Unboxing


                                        Demo on Boxing and Unboxing



Boxing: Converting value type to Reference type is called Boxing.

UnBoxing: Converting Reference type to value Type is called UnBoxing.

Program:

using System;

namespace FreshersLike

{

    class BoxingNUnboxingDemo

    {

        public static void Main()

        {

            int i = 10;     //Value Type Member

            object o;     //Reference Type Member

            o = i;           //Boxing

            int j;             //Value Type Member

            // j = o;  Error

            j = (int)o;     //Unboxing

            int k;         //Value Type Member

            k = i;         //Assignment

            Console.WriteLine("{0} , {1} , {2} , {3} ", i,j,k,o);
        }
    }
}
Posted on 02:08 | Categories: