Thursday 28 February 2013

Need of Abstract classes and Abstract Methods

The concept of abstract classes is an extension to the concept of inheritance. In inheritance the parent classes can be defined with attributes that are commonly required under multiple child classes providing reusability.

FIGURE
  • Rectangle
  • Circle
  • Triangle
  • Cone
In the above case when we want to define different entities representing various figures like Rectangle, Traingle, Circle, Cone etc in such cases first we identify the attributes that are commonly required and put them under a parent class.
The class Figure as it is the parent provides the attributes commonly required to all figures given reusability. Along with this we can also define any abstract in the parent class figure so that the child classes will implement the abstract members which is a restriction for the child classes and cannot escape from it.
   

Created .aspx pages and Connected to the Database

Sudeep gave us a task to create two .aspx pages, and to link the first page with the second page and then asked to connect this pages with the Database. So that if once connected to the database we can Add, Update, Delete the data in the table that is stored in the database.
I have created one table GetJob in my database 'DB1' with the following columns and inserted some values to it:
  • JobCode
  • Vacancy
  • Post
  • Company
  • City
After creating a table I have added two web forms by selecting Asp.Net Empty Web Application in Visual Studio. The forms are named as Entry.aspx and JobDetails.aspx.
In Entry.aspx I have taken a Gridview Control(GridView is use to accept, display and edit data on a web page) and one Button(ADD).
Then I have provided a link to the ADD button that will transfer directly to second page i.e JobDetails.aspx wherein I have created one table with the same number of columns as in the GetJob table and then  within the table i have taken 5  labels and 5 text boxes. Along with that one button(SAVE) to save the data to the database. When we click to SAVE button it saves the data in the database. This I Have done through connectivity with SQL Server.
 Sudeep also told us to make the entire process more clear and asked us to do the following:
  •  Some validations the the text boxes.
  • Assign a Primary key to the first column of a table i.e'JobCode' and also it should generate automatic values once we switch to the form wherein Details need t o add.
  • With the Add link there should be two more links Update and Delete.
  • Instead of taking textbox towards the label 'City' there should be a DropDown List to give a chance to select one from many cities. 
  • After saving a JobDetails that page should redirect to the Entry.aspx page to add another Vacancies if required.

Interfaces

  • This is also a user defined type same as a class but can contain only abstract members in it.
  • The abstract members of an Interface will be implemented by a child class of  the Interface so here also the Interface is imposing restrictions on the child class.
  • A child can be inherited either from another class or an Interface also. When a class is inheriting from another class it can inherit only from one class (Single Inheritance). When class is inheriting from an Interface it can be inherited from any number of interfaces (Multiple Inheritance).
  •  Inheritance is divided into two categories
  1.   Implementation Inheritance
  2.   Interface Inheritance
  • If a class is inheriting from another class we call it as implementation Inheritance and this provides reusibility because child classes can consume their parent class members.
  • If a class is inheriting from an Interface we call it as Interface Inheritance but Interface Inheritance does not provide any reusibility because here the child c lass is only implementing the methods of its parent Interface.
Syntax:
[<modifiers>]  Interface <Name>    
{
 Abstract class declaration
}   

Rules while working with Interfaces
  • We can not declare variables under an interface.
  • Default scope for a member of an Interface is public where as it is private in case of class.
  • Every member of an Interface is by default abstract so we don't require to declare explicitly.
  • An Interface can inherit from another Interface if required.
Add an Interface item template under the project by choosing Interface from the add new item window and name it interface1.cs

interface Interface1
    {
        void add(int x, int y);
        void sub(int x, int y);
    }

Add another interface interface2.cs and write the following code

interface Interface2
    {
        void mul(int x, int y);
        void div(int x, int y);
    }

Now to implement the members of both these interfaces add a class interclass.cs and write the following code
       
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApps
{
    class interclass: Interface1,Interface2
    {
        public void add(int x, int y)
        {
            Console.WriteLine(x + y);
        }
        public void sub(int x, int y)
        {
            Console.WriteLine(x - y);
        }
        public void mul(int x, int y)
        {
            Console.WriteLine(x * y);
        }
        public void div(int x, int y)
        {
            Console.WriteLine(x / y);
        }
        static void Main()
        {
            interclass c = new interclass();
            c.add(100,45);
            c.sub(89,26);
            c.mul(12,34);
            c.div(456,23);
            Console.ReadLine();
        }
    }
}

Wednesday 27 February 2013

Abstract Methods and Abstract Classes

A method without any method body is known as a Abstract method. It will have only the defination for the method the method and to declare any method as abstract we need to use abstract modifier on the method.    
   A class under which we define abstract members is  known as a abstract class and it also has to be defined using abstract modifier.

 abstract modifier
{
   public abstract void Add(int x,int y);
}

The concept of abstract method is near similar to the concept of method overriding were in case of Method Overriding if any members of parent class are declared as virtual those methods can be re- implemented under the child class using the override modifier.

Add a classAbsParent.cs and write the following code:
using System;

namespace CSharpConsole
{
    abstract class AbsParent
    {
        public void Add(int x, int y)
        {
            Console.WriteLine(x + y);
        }
        public void Sub(int x, int y)
        {
            Console.WriteLine(x -y );
        }  

        public abstract void mul(int x, int y);
        public abstract void div(int x,int y);
        
    }
 }
//Add another class AbsChild.cs and write the following code:
using System;
namespace CSharpConsole
{
    class AbsChild: AbsParent
    {
        public override void  mul(int x, int y)
      {
       Console.WriteLine(x*y );
      }

        public override void  div(int x, int y)
      {
       Console .WriteLine (x/y );
      }
        
      static void Main()
        {
            AbsChild c = new AbsChild();
            c.Add(20, 12);
            c.Sub(15, 5);
            c.mul (5, 5);
            c.div (10, 5);
            Console.ReadLine();
      }
    }
}

Tuesday 26 February 2013

Creating my first aspx page

         Yesterday Sudeep gave us the task of creating an aspx page and connecting it to the database. We have started working on it and will try to finish the task as soon as possible.
                

TSQL Programming

  From last two days,we have started learning TSQL Programming. TSQL is non procedural or 4th Generation language because in TSQL we need to specify only what is the problem to solve and no need to specify how to solve the problem step by step hence working with TSQL will be easy but because of non-procedural value it is not possible to solve some complex queries .
  To overcome this problem Microsoft provides programming language feature into SQL which also are collectively called as TSQL Programming.

We have done  some basic programming using :
  •  Conditional Control Statements
  • Looping Control Statements
  • Cursor
Since we completed practicing all basic queries, now we have started with TSQL Programming which is the last part of SQL Server.

Monday 25 February 2013

Operator Overloading

   It is an approach of defining multiple behaviors to an operator just like we can define multiple behaviors to a method in method overloading.
   In method overloading a method will have different behaviors that are defined basing on the parameter types of that method. In the same way in operator overloading, a operator will have different behaviors based on the operands between which we use the operators.
   For example '+' is an overloaded operator which works as additional operator when used between two numeric operands and works as a concatenation operator when used with the string operands or string-numeric operands.
numeric+numeric=addition
string+string=concatenation
string+numeric=concatenation
   Same as we discussed about we can also add new behavior to  existing operators of the language by defining an operator method on that operator as following:
[<modifiers>] static <types> operator <opt> (<operand types>)
    A operator method must b defined as static only. Type refers to the data-type of the value what it returns when the operator is used between two operands. Operator is a keyword which tells we are defining an operator method. <opt> refers to the type of operands between which we want to use the operator.

Example of Operator Overloading


// Add a new class Matrix.cs
using System;
namespace CSharpConsole
{
    class Matrix
    {

        //declaring variables for a 2*2 matrix
        int a, b, c, d;
        public Matrix(int a, int b, int c, int d)
        {
            //initializing the matrix varaibles
            this.a=a ; this.b=b ;
            this.c=c ; this.d=d ;
        }

        // overloading the + operator so that it can be used for adding values of 2 matrix
        public static Matrix operator + (Matrix m1, Matrix m2)
        {
            Matrix obj = new Matrix(m1.a + m2.a, m1.b + m2.b, m1.c + m2.c, m1.d + m2.d);
            return obj;
        }

        // overloading the - operator so that it can be used for substracting values of 2 matrix
        public static Matrix operator -(Matrix m1, Matrix m2)
        {
            Matrix obj = new Matrix(m1.a - m2.a, m1.b - m2.b, m1.c - m2.c, m1.d - m2.d);
            return obj;
        }

        //overriding the ToString method inherited from object class for returning the values associated with matrix object
        public override string ToString()
        {
            return string.Format("[a:{0};b:{1};c:{2}:d:{3}]", a,b,c,d );
        }
    }
}
// Add another class TestMatrix to test the above code.

using System;
namespace CSharpConsole
{
    class TestMatrix
    {
        static void Main()
        {
            Matrix m1 = new Matrix(5, 6, 7, 8);
            Matrix m2 = new Matrix(1, 2, 3, 4);
            Matrix m3 = m1 + m2;
            Matrix m4 = m1 - m2;
            Console.WriteLine(m1);
            Console.WriteLine(m2);
            Console.WriteLine(m3);
            Console.WriteLine(m4);
            Console.ReadLine();
        }
    }
}

Sealed Classes and Sealed Methods

  • Sealed Classes: A Class which is defined by using the sealed modifier is a sealed class but if any class is declared as sealed the class cannot be inherited by any other class 
           for example:
           sealed class class1
           class2:class1    //Invalid
Note: We can still consume a sealed class from any other class by creating the object even if inheritance is not possible.
  • Sealed Methods: A method which cannot be overridden under child classes is called as Sealed Methods and by default every method of a class is a sealed method because overriding a child class under parent class is possible only when the method is declared as virtual.
            If a method is declared as virtual under a class, any child class of the class linear hierarchy has a right to override the method. 
       class1
       public virtual void show()
       class2:class1    
       public override void show()
       class3:class2
       public override void show()
Note: In the above case even if class2 does not override the method then also class3 can override the method.

Method Overriding

If at all a parent classes method is re-implemented under a child class exactly with the same signature we call it as Method  Overriding.

How to override a parent classes method under child class ?
  • If we want to override any parent classes method under child class first under the parent the method must be defined using virtual modifier. Declaring a method as virtual under a class is giving a permission for its child classes to override.
  • The methods that are declared as virtual can be overwritten as child classes using a override modifier.
      class1
      public virtual void Show()  // overridable
      class 2:class1
      public override void Show() // overriding

Add a class LoadParent.cs and write the following code
using System;
namespace CSharpConsole
{
    class LoadParent
    {
        public void Test()
        {
            Console.WriteLine("Parent's test method");
        }
        public virtual void  Show() //  overridable
        {
            Console.WriteLine("Parent's show method");
        }
        public void Display()
        {
            Console.WriteLine("Parent's display method");
        }
    }
}
// Add a class loadchild.cs and write the following code
using System;
namespace CSharpConsole
{
    class loadchild: LoadParent
    {
       // overloading parent's test method
        public void Test(int x)
        {
            Console.WriteLine("Child's test method ");
        }
       public override  void Show()
        {
            Console.WriteLine("Child's show method ");
        }
       public new void Display()
       {
            Console.WriteLine("Child's display method");
       }
       static void Main()
       {
           loadchild c = new loadchild();
           c.Test();
           c.Test(10);
           c.Show();
           c.Display();
           Console.ReadLine();
       }
   }
}

Types of Polymorphism

Polymorphism is divided into 2 categories
  • Compiletime Polymorphism (also called as Static Polymorphism or Early Binding)
  • Runtime Polymorphism (also called as Non-Static Polymorphism or Late Binding)
        In the first case the object of the class will identify for a particular method call which polymorphic method has to be executed and binds the method call with its method definition and executes this method in runtime but only the method will be bound. This type of binding takes place in Overloading because in overloading we have multiple methods with same name but different signature so each method is unique to itself so while binding the method definition we can identify the unique keyword.
       In the second case ie Runtime Polymorphism the object of a class recognizes which polymorphic method it has to call in runtime and this kind of identification takes pace in Overriding and Hiding as there are multiple methods with the same name and same signature with both the cases so that  the object will identify which exact method has to be executed in runtime only being on the hierarchy of the class.

Friday 22 February 2013

Constructor Overloading

Just like we can overload the methods of a class in the same way we can also overload the constructors of a class i.e it is possible to define multiple constructors under a class changing their signatures.
    If a constructor of a class are overloaded we can create object of that class by making use of any constructor available or we can create a separate object by using a separate constructor.

Add a class LoadCon.cs and write the following code.

// Constructor Overloading

using System;
namespace CSharpConsole
{
    class LoadCon
    {
        int x;
        public LoadCon()     //default constructor
        {
            x = 10;
        }
        public LoadCon(int x)
        {
            this.x = x;

        }
        public void Display()
        {
            Console.WriteLine("The value of x is:{0}",+ x);
        }
        static  void Main()
        {
            LoadCon c1 = new LoadCon();
            LoadCon c2 = new LoadCon(20);
            c1.Display();
            c2.Display();
            Console.ReadLine();
        }
    }
}

Polymorphism

Its an approach of definning different methods with a different behaviour i.e whenever the input changes the output or behaviour also changes accordingly which can be implemented under a language using different approaches like Overloading, Overriding and Hiding.

Overloading
This is again of three types:
  • Method Overloading
  • Constructor Overloadding
  • Operator Overloading 
Method Overloading: It is an approach of definnig multiple methods under a class with the same name, by changing the signature of those methods. Changing the signature of those method in the sense we can change the number of parameters being passed to the method or order of parameters being passed to method.

//Method overloading

using System;

namespace CSharpConsole
{
    class LoadDemo
    {
        public void Show()
        {
            Console.WriteLine(1);
        }
        public void Show( int x)
        {
            Console.WriteLine(2);
        }
        public void Show(string s)
        {
            Console.WriteLine(3);
        }
        public void Show(int x, string s)
        {
            Console.WriteLine(4);
        }
        public void Show(string s, int x)
        {
            Console.WriteLine(5);
        }

        static  void Main()
        {
            LoadDemo d = new LoadDemo();
            d.Show();
            d.Show(10);
            d.Show("hello");
            d.Show(5,"leena");
            d.Show("bansiwal", 15);
            Console.ReadLine();
        }
    }
}

Thursday 21 February 2013

Axure Tooltip

         Today, Sudeep showed us how to use a tool called 'Axure'. It is a very powerful wire-framing tool, that lets you create clickable wire-frames very quickly. The Business Analyst acts as a middle-ware between the Software developer and the Client. When a Client places an requests the BA analyses those requests and proceeds the work accordingly.
         We also created 2-3 pages using this tool  under the guidance of Sudeep and it was nice to learn something new.

Extension Methods in CSharp

This is a new feature that has been inter choose in C#.Net 3.0 that allows us to add new methods into an existing class without editing the source code of the class.

Defining Extension:
  •  Extension methods must be defined under static class only so every extension method is static by default but once the method is bound with another class it gets converted into non static and it can be called by using old classes object.
  • The first parameter of an extension method  is to specify to which class the method has to be bound with and we call it as a binding parameter which should be defined as following. 
         this<class name><obj>
  • If required we can also pass normal parameters to an extension method which should start from the second place of parameter list and all this parameter will be considered while calling the method.
  • If an extension method is defined with n parameters while calling the methods we will be having n-1 parameters only because binding parameters is excluded.
Add a class oldclass.cs and code the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
 class oldclass
 {
  public int a;
  public oldclass (int a)
  {
   this.a=a;
  }
   public void show1()
  {
   Console.WriteLine("Method One:  " + this.a);
  }
   public void show2()
  {
   Console.WriteLine("Method Two:  " + this.a);
  }
 }
}
// Now add a newclass.cs and code the following
using System;
namespace CSharpConsole
{
  static  class newclass
 {
  public static void show3(this oldclass obj)
   {
    Console.WriteLine("Method three ");
   }
    public static void show4(this oldclass obj, int x)
   {
    Console.WriteLine("Method four: "  + x);
   }
    public static void show5(this oldclass obj)
   {
    Console.WriteLine("Method five: " + obj.a);
   }
 }
}
// Again add a new classTestold.cs to test the above code

using System;

namespace CSharpConsole
{
 class Testold
 {
   static void Main()
   {
    oldclass obj = new oldclass(10);
    obj.show1();
    obj.show2();
            
    // Calling Extension Methods
     obj.show3();
     obj.show4(50); obj.show5();
     Console.ReadLine();
   }
 }
}

Tuesday 19 February 2013

     Today we practiced the concept of Inheritance, Some SQL queries based on joins, indexes, set operators etc. We also learnt the concept of security in SQL Server, and that how certain permissions or privileges can be granted to other users by the users who own the database etc...
     It was a good day... Learning more and more every day and more important enjoying while learning.

Monday 18 February 2013

Inheritance in CSharp

Children inheriting the property of there parents is known as inheritance which gives reusability we can implement the same rule in our object oriented languages and established parent child relationship between classes so that parent class members can be consumed by child classes i.e child classes are reusing the parent class members without rewriting them again.

A parent class can have any number of child classes and members of parent class can be consumed under all the child classes.

Private members of a class cannot be inherited and acquired by the child classes.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class Class1
    {
        public Class1()
        {
            Console.WriteLine("Class1 constructor called");
        }
        public void test1()
        {
            Console.WriteLine("First Method");
        }
        public void test2()
        {
            Console.WriteLine("Second Method");
        }

    }
}// Now add another class Class2.cs and write the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class Class2:Class1
    {
        public Class2()
        {
            Console.WriteLine("Class2 constructor called");
        }
        public void Test3()
        {
            Console.WriteLine("Third method called");
        }

        static void Main()
        {
            Class2 c = new Class2();
            c.test1(); c.test2(); c.Test3();
            Console.ReadLine();
         }
    }
}


Each day learning something New and Interesting

Today, we spent more time upon writing SOL queries. Created Sub queries under that I did :
  • Single Row Sub Queries.
  • Multi-Row Sub Querries.
  • Nested Sub querries And
  • Co-related sub queries. I also created Indexes . Indexes is a relational database are mainly use to make the data retrival faster similiar to index available at the end of the book. There are two types of Indexes:
  • Clustered Index : Cluster index is the index that will arrange the rows physically in the memory in sorted order.
  • Non-Clustered Index : Cluster index is the index that will not arrange the rows physically in the memory in sorted order.
So I created the Index by name DnoIdx on Emp table that I created before and tried both clustered/non clustered creation of Indexes.
In the secod half I tried to analyze and learn C# basics like:
  • Static Methods vs Non Static Methods
  • Static Constructors vs Non Static Constructors
  • Static Classes
It has become the everyday Routine to learn Something new related to SQL Server and C#.Net and implement it practically.Looking forward for more such new topics everyday, so that I can at the end, able to know almost everything about the .Net language concepts(C# and ASP),AJAX and SQL Server far better.

 

Static Constructor Vs Non-Static Constructor

  • A Constructor that is defined using static keyword is a static constructor. Rest of the others are Non-Static only. 
  • Static Constructor is the first block of code which executes under a class where as Non-Static Constructor gets executed only after creating the object of class as well as each and every time the object of class is created.
  • In the life cycle of a class a Static Constructor gets called one and only one time whereas a Non-Static Constructor gets called either for zero(no objects are created) or n times (n objects are created).
  • A Static Constructor can never be parametrized because it is the first block of code that executes in a class and more over it is implicitly called and Non-Static constructors can be parametrized because they are explicitly called and we have a chance of sending values to the parameters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApps
{
    class StatCon
    {
        static StatCon()
        {
            Console.WriteLine("Static Constructor Called");
        }
        public StatCon()
        {
            Console.WriteLine("Non-Static Constructor Called");
        }
        static void Main()
        {
            Console.WriteLine("Main Method Called");
            StatCon s1 = new StatCon();
            StatCon s2 = new StatCon();
            StatCon s3 = new StatCon();
            Console.ReadLine();
        }
    }
}

Sunday 17 February 2013

Static Methods vs Non Static Methods

  • A method which s defined using the static modifier is a static method is a static method otherwise all the other methods are non static only.
  • While defining static methods under a class we need to make sure that we are not consuming any non static members of class under a static method because direct consumption of non static is not possible under static methods, to consume hat first we need to create the object of class and with that object only non static members can be consumed under static block.
RULE :  Static members of a class can be consumed under non static blocks of the class directly but non static members must be consumed in a static block only  with the help of class object.
using System;

namespace CSharpConsole
{
    class StatMets
    {
        int x = 100;
        static int y = 200;
        static void Add()
        {
            StatMets obj = new StatMets();
            Console.WriteLine(obj.x + y);
                
        }
        static void Main()
        {
            StatMets.Add();
            Console.ReadLine();
        }
    }
}

Friday 15 February 2013

End of Week

Today, the end of the great week. Had posted some Question answers in the MindQ, practiced some CSharp basic programs etc.  Also today, I played carom(its a indoor game) in my floor itself  where other colleagues use to play it for refreshment. Today even I played with them. I still remember the last time I played carom, around 3-4 years back. And after a long period I got the chance to play again over here, and guess what? I won the game. It was fun. One of the good experience in Inforica.

The Concept of 'Joins' in SQL Server

Combining data from multiple tables using single select statement is called as a JOIN.
Joins are classified into
  • Inner Join.
  • Outer Join
  • Cross Join
 Inner Join are again classified into:
  • Equi Join
  • Natural Join
  • Non-Equi Join
  • Self Join
Outer Join are classified into
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Cross Join
  1. Inner Join:  Inner Join is the join that can display only the rows that satisfy the given join condition.
  2. Equi Join: The Inner Join that uses equal operator in the join condition is called as Equi Join.
  3. Natural Join: An inner join is called as natural join only when it satisfies the following three condition.
  • Join is Equi Join.
  • All common columns in the table must be in the joined condition.
  • Only one set of common columns is displayed in the output.
   4. Non Equi Join: The inner join that uses the operator other than equal in the join condition is called as non-Equi Join.

    5.  Self Join: A join that joins a taable to its self is called as Self Join.
  • Outer Join:  Outer Join is a join that can display the rows that does not satisfy the given join condition along with the rows that satisfy the given condition.
  1. Left Outer Join: Left outer Join is an Outer Join that can display the rows that does not satisfy the given condition only from left hand side table. To perform Left outer join with ANSI syntax use the keyword Left Outer Join and to perform Left Outer Join with non-ANSI syntax use the keyword *= in the join condition.
  2. Right Outer Join: Right outer Join is an Outer Join that can display the rows that does not satisfy the given condition only from right hand side table. To perform right outer join with ANSI syntax use the keyword Left Outer Join and to perform Right Outer Join with non-ANSI syntax use the keyword =* in the join condition.
  3. Full Outer Join:  Full outer Join is an Outer Join that can display the rows that does not satisfy the given condition only from both the tables. To perform full outer join with ANSI syntax use the keyword full Outer Join and to perform Full Outer Join with non-ANSI syntax perform union on left outer join and right outer join statements.
  4. Cross Outer Join: The join that joins every row in the first table with every row in the second table is called as Cross Join.

Constant Variables And Read only variables

 Constant Variables
  • Its a variable whose value cannot be modified once after declaration so it is compulsory to initialize the variable at the time of declaration so it is compulsory to initialize the modified once after declaration so it is compulsory to initialize the variable at the time of declaration only and to declare a variable as constant we need to use const
  • The behavior of a constant variable will be similar to the behavior of static variables i.e maintains a single copy by initializing one and only one time immediately once the execution of class starts.
 Read only variables
  • Its a variable whose value cannot be modified once after initialization and should be declared using readonly keyword.
  • As readonly variables cannot be initializing after declaration also i.e it can initialized under a constructor.



Static Modifier

This is used for declaring the members of the class as well as the class as static. As we aware that a class is a collection of members and those members are divided into two categories :-
  • Static Members
  • Non-static Members
The  members of a class which doesn't require the object of the class for initialization or execution are known as static members, whereas the members of class which requires object of class for initialization and execution are known as non static members.

Note:  Non static members can also be called as instance method because they are associated with an object or instance of class.

Static members vs non static variables
  • A variable that is declared using static modifier or a variable that is declared under any static block is a static variable rest of the other are non-static only.
         int x=100                  // non static
         static int y=200         //static
         static void  Main()
        { 
          int z=300;               //static
        } 
  • A static variables get initialized immediately once the execution of class starts and whereas a non-static variables get initialized on when the object of the class is created as well as each time the object is created.
  •    A static variable get initialized one and only one time in the life cycle of class whereas a non static variables mat be initialized for 0 or n times.

TestMath.cs

To test the below, add a new class TestMath.cs and write the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class TestMath
    {
        static void Main()
        {
            Math m = new Math(100,50);
            m.Add(100, 50);
            m.Sub(60, 30);
            m.Mul(10, 20);
            m.Div(50, 25);
            Console.ReadLine();
           
        }
    }
}

What is the need of defining a Constructor explicitly?

 We are already aware that if we don't define a constructor in the class, still a implicit constructor comes  into picture and initializes some variables of class but we explicitly defines constructor under class so we have a chance of initializing variables of class with desired values.

Each and every class requires some initialization values foe execution so if we defined a constructor under the class we have a chance of sending our own initialization values for the class to execute and this values can be changed whenever wee create a new object for the class.

In the below below case, the four methods of the class requires the values for x and y to execute and those two variables are being initialized under the constructor so when and where we want to consume this class we can send values for x and y while creating the objects and then call all the four methods:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class Math
    {
        int x, y;
        public Math(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public void Add(int x, int y)
        {
            Console.WriteLine(x + y);
        }
        public void Sub(int x, int y)
        {
            Console.WriteLine(x - y);
        }
        public void Mul(int x, int y)
        {
            Console.WriteLine(x * y);
        }
        public void Div(int x, int y)
        {
            Console.WriteLine(x / y);
        }
       
     }
}

Thursday 14 February 2013

When ever an object of class is created the constructor of that class gets called and the required memory get allocated. While creating the object of a class we are explicitly calling the constructor as following

  ConDemo cd = new ConDemo();  (Calling the constructor)

Constructors are of two types
  • Parameter-less  (zero argument) Constructor.
  •  Parametrized Constructor.

  •   Parameter-less  (zero argument) Constructor:   A constructor without any parameter is a parameter-less constructor which can be either defined implicitly provided there is no explicit constructor or else can be defined explicitly.
  •  Parametrized Constructor: A constructor with parameters is a Parametrized Constructor and can be defined only explicitly. if the constructor of a class is  Parametrized values to the parameter are sent while calling the constructor.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApps
{
    class ConParam
    {
        int a;   // a is a class variable
        public ConParam(int a)   // a is a block variable
        {
            this.a = a;
        }
        public void display()
        {
            Console.WriteLine("Values of a is{0}", a);
        }
        static void Main()
        {
            ConParam p1 = new ConParam(10);
            ConParam p2 = new ConParam(20);
            ConParam p3 = new ConParam(30);
            p1.display();
            p2.display();
            p3.display(); 
            Console.ReadLine();
        }
     }
}
Constructors:

      It is a special method present under a class which is responsible for initializing the variables of class. The name of constructor method will b same as the class name and more ever it is a non value returning method.

Syntax to define a constructor:
[<modifiers>] <name> ([<parameter defination>])
   {
        Statements;
   }

      Every class requires a constructor to be present in it, if we want to create an object of that class.

Note: As the constructor is mandatory for creating the object of a class, it must be defined by the programmer explicitly, or else while compiling the program compiler takes the responsibility of defining a constructor implicitly under that class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApps
{
    class ConDemo
    {
        public ConDemo()
        {
            Console.WriteLine("Constructor is called");
        }
        public void demo()
        {
            Console.WriteLine("Method is called");
        }
        static void Main()
        {
            ConDemo cd1 = new ConDemo();
            ConDemo cd2 = new ConDemo();
            ConDemo cd3 = cd2;
            Console.ReadLine();
        }
    }
}

Working with Objects in CSharp

If required the object of a class can be assigned to the variable of the same class and initialized it, so that the variable becomes reference of the class but references of a class will not have separate memory allocation. They will be provided with access to the memory of object and reference will be consuming the same memory. To test this rewrite the code under Main method of class first as following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class First
    {
        int x = 100;
        static void Main()
        {
            First f1 = new First(); // f1 is a object
            First f2;              // f2 is a reference
            f2 = f1;
            Console.WriteLine(f1.x + "   "+ f2.x);
            f1.x=300;
            Console.WriteLine(f1.x + "   "+ f2.x);
            f1.x = 500;
            Console.WriteLine(f1.x + "   " + f2.x);
            Console.ReadLine();
        }
     }
}
/* In the above case, as the object and reference were consuming the same memory
 * any modifications that are performed on the members through 1 reflects on the
 * other also*/
We can create any number of objects to a class and each object we create will have separate memory allocation and any object doesn't reflect to the member's of other object. To test this rewrite the code under the Main method of class first as following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class First
    {
        int x = 100;
        static void Main()
        {
            First f1 = new First();
            First f2=new First();
            Console.WriteLine(f1.x + "   "+ f2.x);
            f1.x=500;
            Console.WriteLine(f1.x + "   "+ f2.x);
            Console.ReadLine();
        }
     }
}
The object which is created can also be destroyed by assigning null to it. Once we assigned null to an object the memory which is allocated for object is marked as unused and cannot be accessed anymore. To test this rewrite the code under Main method of First class as following:

Note: The memory which is marked as unread will be deallocated by the Garbage Collector whenever it comes into a picture for deallocation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class First
    {
        int x = 100;
        static void Main()
        {
            First obj = new First();
            Console.WriteLine(obj.x);  // Prints the value here
            obj = null;
            Console.WriteLine(obj.x);  // Error occurs here
            Console.ReadLine();
        }
     }
}
Every member of a class(variable or method) it it is non--static can be accessed under the Main method only by using object of class. A variable of a class is a copy of the class which is not initialized.
                        obj ----> null

An object of a class is a copy of a class which is initialized using new keyword and the memory is allocated for an object of class.
                        obj --->100=x

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class First
    {
        int x = 100;
        static void Main()
        {
            First obj = new First();
            Console.WriteLine(obj.x);    //obj is a object or instance 
            
            Console.ReadLine();
        }
     }
}
// It will print the value of x=100

Wednesday 13 February 2013

Posted Questions In mindqjobs.

        Today Sudeep told us to post some questions along with their answers on the site mindqjobs.com. The questions were divided into various sections like manual testing, SQL, Quick test professional, LoadRunner etc. We posted the questions so that any person who is registered in minqjobs can view them and can improve his/her technical knowledge.

Tuesday 12 February 2013

Calling members of another class


//calling methods of another class by creating object

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class Testclasses
    {
        static void Main()
        {
            Method_ex m1= new Method_ex();     // m1 is a object created
            Params p1=new Params();            // p1 is a object created
            m1.Test1();                        // calling methods
            m1.Test2(6, 12);
            Console.WriteLine(m1.Test3());
            Console.WriteLine(m1.Test4("Hello"));
            p1.AddNums(100, 75, 50);

            int x = 0, y = 0;
            p1.Math1(100, 25, ref x, ref y);
            Console.WriteLine(x + "   " + y);
            p1.Math2(100, 50, out x, out y);
            Console.WriteLine(x+ "   "+y);
            Console.ReadLine();              // waiting to key press
        }

    }
}

Operator Overloading



// Write a prorram to print the sum of complex numbers


using System;

public struct Complex
{
    public int real;
    public int imaginary;

    public Complex(int real, int imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }

    // Declare which operator to overload (+), the types 
    // that can be added (two Complex objects), and the 
    // return type (Complex):
    public static Complex operator +(Complex c1, Complex c2)
    {
        return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
    }
    // Override the ToString method to display an complex number in the suitable format:
    public override string ToString()
    {
        return (String.Format("{0} + {1}i", real, imaginary));
    }

    public static void Main()
    {
        Complex num1 = new Complex(2, 3);
        Complex num2 = new Complex(3, 4);

        // Add two Complex objects (num1 and num2) through the
        // overloaded plus operator:
        Complex sum = num1 + num2;

        // Print the numbers and the sum using the overriden ToString method:
        Console.WriteLine("First complex number:  {0}", num1);
        Console.WriteLine("Second complex number: {0}", num2);
        Console.WriteLine("The sum of the two numbers: {0}", sum);
        Console.ReadKey();

    }
}

Parameters in CSharp

      Parameters are defined so that the methods can be made more dynamic. Parameters of a methods can be of 2 types.
  •  Input parameters.
  • Output parameters.
       Input parameters are used for bringing values in the method for execution. Whereas output parameters are used for carrying a value ouut of the method after execution of the method
       By default every parameter we pass to a method is an input parameter and if we want to define a parameter as output parameter we need to prefix the parameter with ref or out keyword.

      public void test (int x,ref int y,out int z)

       Here x is an input parameter and y& z are output parameters.

Note: By using return types also we can send results out of a method, but only a single result whereas if we are sending results out of a method using output parameters we have a chance of sending more than 1 result at the time of execution.

Monday 11 February 2013

Using Parameters


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class Params
    {
        //method with default value to parameterss
        public void AddNums(int x, int y = 50, int z = 25)
        {
            Console.WriteLine(x + y + z);

        }
        //method with both input and output parameterss
        public void Math1(int a, int b, ref int c, ref int d)
        {
            c = a + b;
            d = a * b;
        }
        public void Math2(int a, int b, out int c, out int d)
        {
            c = a + b;
            d = a * b;
        }
        static void Main()
        {
            Params p = new Params();
            // Calling method with default values to parameters
            p.AddNums(100);
            p.AddNums(100, 100);
            p.AddNums(100,z: 100);
            p.AddNums(100, 100, 100);

            // Calling methods with input and output parameters
            int x = 0, y = 0;
            p.Math1(100, 50, ref x, ref y);
            Console.WriteLine(x + "   " + y);

            int m, n;
            p.Math2(200, 25, out m, out n);
            Console.WriteLine(m + "   " + n);
            Console.ReadLine();

        }
    }
}

// Program to print the following pattern
//   *****
//   *****
//   *****
//   *****
//   *****


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pattern_4
{
    class Program
    {
        static void Main(string[] args)
        {
            int i,j, k;

            for (i = 1; i <= 5; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write("");
                }
                for (k = 1; k <= 5; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();

            }
            Console.ReadKey();
        }
    }
}
  

Sunday 10 February 2013


// Write a program to print the binary format of a number

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int num;
            Console.Write("Enter a Number : ");
            num = int.Parse(Console.ReadLine());
            int quot;

            string rem = "";

            while (num >= 1)
            {
                quot = num / 2;
                rem += (num % 2).ToString();
                num = quot;
            }

            // Reversing the  value
            string bin = "";
            for (int i = rem.Length - 1; i >= 0; i--)
            {
                bin = bin + rem[i];

            }

            Console.WriteLine("The Binary format for given number is {0}", bin);


            Console.Read();

        }
    }
}

Methods in CSharp


//Using Methods(Example)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class Method_ex
    {

        //Non value returning method without parameter
        public void Test1()
        {
            int m = 5;
            for (int i = 1; i <= 10; i++)
                Console.WriteLine("{0}*{1}={2}", m, i, m * i);
        }
        // Non value returning method with parameters
        public void Test2(int m, int n)
        {
            for (int i = 1; i <= n; i++)
                Console.WriteLine("{0}*{1}={2}", m, i, m * i);
 
        }
        //value returning method with out parameters
        public string Test3()                  //Static action
        {
            string str = ("Hello Leena");
            str = str.ToUpper();
            return str;
        }
        //value returning method with parameters
        
           
        public string Test4(string str)       
        {                        
            str = str.ToUpper();             //Dynamic Action
            return str;
        }
            
     static void Main(string[] args)
      {
         Method_ex m=new Method_ex();

        //calling non-value returning methods
         m.Test1();
         m.Test2(7,20);
        //Calling value returning methods
         string str=m.Test3();
         Console.WriteLine(str);
         Console.WriteLine(m.Test4("leena"));
         Console.ReadLine();     
         
      }
    }
}

Friday 8 February 2013

Watched Videos!!!

       Today was a day with practicing some c# programs, some SQL queries and also i saw some videos on C# console application.
      I found one very good link on C# windows programing which is given below
                         Videos of windows programming in C#

Jagged Array


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace jagged_arrray
{
    class Program
    {
        static void Main(string[] args)
        {
             int[][] arr = new int[4][];
            arr[0] = new int[6];
            arr[1] = new int[7]; 
            arr[2] = new int[8];
            arr[3] = new int[4];
            // printing values of jagged array using nested for
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                    Console.Write(arr[i][j] + "");
                Console.WriteLine();
            }
            // assigning values of a jagged array using nested for
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                    arr[i][j] = j + 1;
            }
            Console.ReadKey();
           

        }
    }
} 

Two Dimentional Array


//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TD_array
{
    class td_array
    {
        static void Main(string[] args)
        {
            int[,] arr=new int[3,5];
            // Can also initialize the values at the time of declaration
            /*int[,] arr = {{11,12,13,14,15},
                         {21,22,23,24,25},
                         {31,32,33,34,35},
                         };*/

            int x = 0;

            //printing values of 2D array using foreach loop
            foreach (int i in arr)
                Console.Write(i + "  ");
            Console.WriteLine("\n");

            //assigning values to 2D array using nested for
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    x = x + 5;
                    arr[i, j] = x;
                }

            }
            //printing values of 2D array using nested for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                    Console.Write(arr[i, j] + "  ");
                Console.WriteLine();
                Console.ReadKey();

            }
        }
    }
}

Example using Array Class


/* Array class- It is a predefined class of base class libraries present
 * under the System namespace that provides a set of members using which 
 * we can perform operations like Sorting, Reversing,Copying etc*/
//Example of Array class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SD_array2
{
    class sd_array2
    {
        static void Main(string[] args)
        {
            //assining a value at the time of declaration
            int[] arr = { 19, 23, 71, 93, 58, 37, 62, 87, 5, 49, 16, 8, 35, 2 };
            for (int i = 0; i < arr.Length; i++)
                Console.Write(arr[i] + " ");
            Console.WriteLine();
           
            {
                Array.Sort(arr);

                foreach (int i in arr) ;
                Console.Write(i + "   ");
                Console.WriteLine();

                Array.Reverse(arr);
                foreach (int i in arr)
                    Console.Write(i+ "  ");
                Console.WriteLine();

                int[] brr = new int[10];
                Array.Copy(arr, brr, 5);
                Console.Write(i + "  ");
                Console.WriteLine();
            }
            Console.ReadKey();

        }
    }
}

Arrays in C#.Net(Single Dimentional Array)


//
// Example of One dimentional array

// Example of Single dimentional array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SD_array
{
    class sd_array
    {
        static void Main(string[] args)
        {
            int [] arr= new int[6];
            int x=0;
            //accessing values of an array using for loop

            for(int i = 0; i <6 data-blogger-escaped-10="" data-blogger-escaped-accessing="" data-blogger-escaped-an="" data-blogger-escaped-arr="" data-blogger-escaped-array="" data-blogger-escaped-assigning="" data-blogger-escaped-console.readkey="" data-blogger-escaped-console.write="" data-blogger-escaped-console.writeline="" data-blogger-escaped-each="" data-blogger-escaped-for="" data-blogger-escaped-foreach="" data-blogger-escaped-i="" data-blogger-escaped-in="" data-blogger-escaped-int="" data-blogger-escaped-loop="" data-blogger-escaped-of="" data-blogger-escaped-pre="" data-blogger-escaped-to="" data-blogger-escaped-using="" data-blogger-escaped-values="" data-blogger-escaped-x="">

Thursday 7 February 2013

Resume Post

        Mindqjobs is a very good site and is very useful in job search.Today I have completed making my resume in mindqjobs.
        Given below is a link to my resume.
                           My Resume

MindQJobs (Resume Post)








.

Its a wonderful site, wherein one could find the upcoming vacancies in the top most Companies and can get many related stuffs. The only thing needed to take full advantage of this site is to Get Registered (Free Registration) with the site.
  • Today, I have completed my Resume in the MindQJobs.

Tuesday 5 February 2013

Another Day With Queries

        Today was another day of practicing SQL Queries. We found SQL quite interesting and enjoyed it a lot though it is just the beginning and we have to learn a lot.

Monday 4 February 2013

C#.Net

C#(pronounced "C sharp") is a programming language that is designed for building a variety of applications that run on the .Net Framework. C# is simple, powerful, type-safe and object-oriented. The .Net Framework class libraries provides access to many operating system services and other useful, well-designed classes that speed up the development cycle significantly. It was developed by Microsoft as part of the .Net initiative and later approved as a standard by ECMA(ECMA-334) and ISO(ISO/IEC 23270)

Design Goals
  • C# language is intended to be a simple, modern, general-purpose, object-orinted programming language.
  • The language, and implementation thereof, should provide support for software engineering principles such as strong type checking, array bounds checking, detection of attempts to use uninitialized variables, and automatic productivity are important.
  • The language is intended for use in developing software components suitable for deployment in distributed environments.
  • Source code copmatability is very important, as is programmer productivity, especially for those programmers already familiar with C and C++.
  • Support for internationalization is very inportant.
  • C# is intended to be suitable for writing applications for both hosted and embedded systems, ranging from the very large that use sophisticated operating systems, down to the very small having dedicated functions. 
C# 2.0 New  Features              C# 3.0 New Features                    C# 4.0 New Features
                                                                                                                                      1.Generics                                1.Implicitly typed local variables.       1.Dynamic bindings.
2.Partial types                           2.Object and collection Initializers.    2.Named and optional arguments.
3.Anonymous methods              3.Auto-Implemented                        3.Generic co-variance and
4.Iterators                                       properties.                                         contra variations.
5.Nullable Types                       4.Anonymous types.
6.Private setters                        5.Extension Methods.
    (properties)                           6.Lambda expressions.
7.Method group                        7.Query expressions.
   conversations(delegates)         8.Expression trees

 C# Features
  • There are no global variables or functions. All methods and menbers must be declared within classes.
  • Local variables cannot shadow variables of the enclosing block, unlike C and C++, C#  supports a strict boolean data tpye, bool.
  • In c# memory adress ponters can only be used within blok specially marked as unsafe, and programs with unsafe code need appropriate permissions to run.
  • c# is more typesafe than C++.
  • C# provides syntactic sugar for a common pattern of a pair of methods, accessor and mutator encapsulating operations on a single of a class, in form of properties.
  • Full type reflection and discovery is available.
C# Advantages
  • C# borrows concepts from Java and C++, adopting only the good bits from those languages and eliminating overly confusing and error prone features, which are the major sources of bug in a code.
  • C# is a terse language.Its very tiny even with the commands.
  • C# supports effective and reusable componets.
  • C# can programs can be written in as simple as a textpad and a command line which are common to any  operating system provided the developer has installed the CLR and the Framework prior.
  • C# provides the ability of code extension to the developers can produce extensions and wrappers to use the  underlying library to behave the way developer want it to.
C# Disadvantages
  • C# progrmas are largely for Microsoft Windows Environment which although happens to be the post widely used Operating system over te globe, but with open source  technologies, especially platforms like Linux cathing up, C# still has to long way to go. This is one major shortcoming of C#.
  • C# has eliminatwd the"unsafe" code funtionalities from C++; it has its own set of functionalities which are to be used with caution by the user.
  • Although C# programs can be written as little as a text editor and compiled using command line, it still needs the .Net Framework to be present on the system , which cripples down the portability in the development of C# programs by a large amount,  and makes it more Microsoft Windows dependent.