Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Thursday, June 7, 2012

Binding Generic List<> Class to the Gridview using DataReader or Datatable

Here now we will fetch the data of a table from Database and bind it to Gridview using ListClass.

First Create a Class

public class Buzz
    {
        private string title;


        public string Title
        {
            get { return title; }
            set { title = value; }
        }


        private string desc;


        public string Desc
        {
            get { return desc; }
            set { desc = value; }
        }
    }


    public class BuzzList : List<Buzz>
    { }


In the above Class Buzz,it takes to members title and description.And also we can see another class BuzzList which is inherited from List<Buzz>


Now in the PageLoad

SqlConnection con = new SqlConnection("ur db connectionstring parameters");
 SqlCommand cmd = new MySqlCommand("select * from BUZZ", con);
 //BUZZ is the table in Database
  con.Open();
        try
        {
            BuzzList objBuzzList = new BuzzList();
            
           SqlDataReader dr = cmd.ExecuteReader();


            while (dr.Read())
            {
                Buzz objBuzz = new Buzz();


             objBuzz.Title = dr["TITLE"].ToString(); 
            //TITLE is the Column in BUZZ table
             objBuzz.Desc = dr["DESCRIPTION"].ToString();
            //DESCRIPTION is the Column in BUZZ table


                objBuzzList.Add(objBuzz);
            }


            GridView1.DataSource = objBuzzList;
            GridView1.DataBind();
        }









Friday, February 18, 2011

Wednesday, December 23, 2009

Necessity for Object Oriented Programming Language.

In simple terms, why we have moved from C(Procedural Language) to C++(Object Oriented Language) ?

In C we use user defined data types like Structure, Union etc, where as in C++ we use classes, here I will try to answer the above question in terms of using these different language elements:

1. The variables we use in a structure or union can be accessed any where in the program, this means, that there is no proper security for data in C i.e. Procedural Langauage. Where as in c++ we have access specifiers like public, private, protected etc, which allows us to restrict the access to a particular variable. So unlike C, C++ has some control over the access to the data.

2. The variables/ elements of one structure are cannot be accessed in an other structure unless they are nested, so there is some access limitations for data in C. Where as in C++ the variables declared and used in one class are allowed/ restricted to get accessed by other classes throught the same access specifiers public, private, protected etc.

3. Function concepts is not supported by structures in C i.e, we cant declare a function as a structure member, where classes can have functions as their members.

These are a few reasons, where there is a necessity to move from a structure oriented language to a object oriented language.

Monday, December 21, 2009

Inheritance

Inheritance is a process to create new class from an exiting class.

Or

Inheritance is the process that allows the reuse of an exiting class to build a new class.

Mainly,Inheritance is based on the principal of code reuse.Instead of creating a class from scratch,it is possible to add new methods,properties and events (events are responce to user actions.)to exiting classes,thus saving time and efforts.The class on which the new class is created is called Base class or Parent class and the class that is created is called Derived, Sub or Child class.

Example:

using System;

namespace InheritanceDemo1
{
//Base class
public class Person
{
private string_name;
private int_age;
public void GetInfo()
{
Console.WriteLine("Enter your name = ");
_name = Console.ReadLine();
Console.WriteLine("Enter your age = ");
_age = int.Parse(Console.ReadLine());
}
public void DispInfo()
{
Console.WriteLine("Dear {0},Your age is {1}",_name,_age) ;
}
}

//Derived class
public class Student:Person
{
private string_school;
private int_english;
private int_math;
private int_science;
public void GetMarks()
{
Console.WriteLine("Enter school names = ");
_school = Console.ReadLine();
Console.WriteLine("Enter marks for English,Maths,Science");
_english = int.Parse(Console.ReadLine());
_math = int.Parse(Console.ReadLine());
_science = int.Parse(Console.ReadLine());
Console.WriteLine("Total Marks obtained : {0}",_english+_math+_science);
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Student objstudent = new Student();
objstudent.GetInfo(); //Accessing base member
objstudent.DispInfo(); //Accessing base member
objstudent.GetMarks();
}
}
}

Base Keyword
The keyword base is used to access members of the base class from inside a derived class.To call a method of the base class is possible even if it has been overridden in the derived class by using the keyword base.Also,creating an instance of a derived class
The constructor of the base classes can be called using base keyword
Constructor, an instance method of an instance property accessory only of the base class can be accessed using the keyword base.

Example:

using System;

namespace InheritanceDemo1
{
public class CA
{
private int priA;
public CA()
{ priA =100;
}
protected void fun(){
Console.WriteLine("From CA"); }
}
public class CB:CA {
private int priB;
public CB(){
priB = 200; }
public void foo(){
base.fun();
Console.WriteLine("From CB");
}
public class UsingBaseKeyword
{
statci void Main(string[] args) {
CB b = new CB();
b.foo(); }}}

* Calling base class constructor by using derived class using base keyword.

Example:

using System;
namespace InheritanceDemo1
{
//Base class
public class Person
{
private string_name;
private int_age;
//base supplies constructor to perform initialization
public Person(string name,int age)
{
this._name = name;
this._age = age;
Console.WriteLine("Name = {0} ",name);
Console.WriteLine("Age = {0} ",age);
}
}


//Derived class
public class Student:Person
{
private int _id;
public Student(String name,int age,int id):base (name,age)
{
this._id=id;
Console.WriteLine("Id = {0} ",id);
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Student objstudent = new Student("Raj",24,4);
}
}
}


Method Overriding in C#

Overriding :- Overriding a method of base class means changing its implementation or overwriting it in the derived class.
*Override keyword:
The override keyword is used to modify a method.An override method provides a new implementation of the base method.For this the base class method has to be declared virtual.Adding virtual to a base class method marks it to for overriding its implementation in the derived class.By default, methods are non-virtual in C# and hence they cannot be overridden.

Note:-
1. The overridden method in a derived class should have the same signature as the base class method it is overriding.
2. The keywords new,static,virtual cannot be used along wirg the override modifier.

*Virtual keyword :
C# provides a keyword virtual that is used the definition of a method to support polymorphism.The virtual keyword is used to modify a method declaration in a class.Such method is called a virtual method.The child classes are now free to implement their own versions of thr virtual method

using override keyword.
The Syntax:-
[Access modifier]virtual[return tyoe]name([parameter list])
{
//Virtual method implementation.
}

Note: The virtual modifier cannot be used with modifier like static and override.
Ex:-
Public virual void function_name()
{
Console.WriteLine("This is a virtual method and can be overridden");
}


* When a virtual method is called ,a runtime check is made to identify the object and the appropriate method is invoked.In case of non-virtual methods,this information is available at compile time,so no runtime check to identify the object is done.
* "new" keyword:- The new keywordmis used as an operator or as a modifier,This section is discussingthe new modifier.The new modifier is used to explicitly hide a member that is inherited from the base class.This means in case if the derived class has a member with the same name as a member in the parent class,then new distinguishes the derived class member as a completely new member.

* Note:- It is an error to use both new an override on the same method.
* Note:- To conclude ,the real benefit of "new modifier" is to be sure that the intention is to hide the base method and this did not happen by accident or misspelling
* Conversely,if a method is declared as new and it does not really hide a base method ,the compiler will also issue a warning that can be suppressed by removing the new modifier.