The blog has moved to the new site F10Debug.com

Thursday, March 24, 2016

Method Overriding Vs Method Hiding Using example in C#

Method Overriding & Method Hiding:

Explanation: 

-  Method overriding / Hiding occurs when child class declares a method with the same name and 
    same Method Signature as parent class.

   In below example derived class has method (overrides method) print, which has same signature as 
   print method of Parent class  (BaseClass) 

- Base class method must be virtual or overridable. 

  Here in our example Base class method name Print() is virtual.

- Child Class Method must have override keyword before return type.  // Case 1: Method Overriding
   
   Here in our example child class method name Print() having override keyword before return type.

- Child Class Method must have New keyword before return type or no keywords before it.  // Case 2: Method Hiding
   Here in our example child class method name Print1() is New & in case of print2() method it has no keywords.
- Method overriding is also called as Run Time Polymorphism/ Dynamic Polymorphism/Late Binding.

Example of Method Overriding & Method Hinding.



using System;

namespace MethodOverriding_Hiding
{

    public class BaseClass
    {
        public virtual void Print()
        {
            Console.WriteLine("Base class method - Print");
        }

        public virtual void Print1()
        {
            Console.WriteLine("Base class method - Print1");
        }

        public void Print2()
        {
            Console.WriteLine("Base class method - Print2");
        }
    }

    public class DerivedClass : BaseClass
    {
        public override void Print()
        {
            Console.WriteLine("\n" + "Derived class method - Print"); // '/n' for new line.
        }

        public new void Print1()
        {
            Console.WriteLine("Derived class method - Print1");
        }

        public void Print2()
        {
            Console.WriteLine("Derived class method - Print2");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Normal method - It will call respective class method only 
            // No matter of virtual/ override / New keyword
            BaseClass B = new BaseClass();
            B.Print();
            B.Print1();
            B.Print2();

            DerivedClass D = new DerivedClass();
            D.Print();
            D.Print1();
            D.Print2();

            //Method Overriding
            BaseClass B1 = new DerivedClass();
            // Base - virtual, Derived - Override -> Child Class (Overriden) method will be 
            // called & its Called as Method Overriding
            B1.Print();

            // Base - virtual, Derived - New or Void -> Base class (Hidden) will be called
            // & its Called as Method Hidding
            B1.Print1();
 
// Base - void, Derived - void -> Base class (Hidden) will be called
B1.Print2();
        }
    }
}

OUTPUT:



Difference Between Method overriding vs. Method Hiding

  • In both cases parent and child class should have same method name and same signature
  • only difference is in case of Method overriding derived class should have overrides keyword
  • and in case of method hiding derived class should have New keyword.




When to use Method Overriding & Method Hiding using real time example

Sunday, November 8, 2015

Not Exists in SQL / How to check if column exists in SQL Server table

Not Exists in SQL Server / How to check if column exists in SQL Server table

Description:

This "Not Exists" keyword we generally used while inserting a new row in table. If that row (depend on keys we passed in where condition) is not exits than insert a row.

Syntax:

IF Not Exists (SQL Query)
Begin
   // Insert statement or do as per requirement. 
End

Example:

Take example of Registration page of Gmail while opening new account. It will check for that particular Email is already exists or not, if not then only it will allow you to create a account else it will ask you to choose another.

IF Not Exists (Select 1 from Users where UserEmail= 'jatin@dotnetaddas.com')
Begin
   // If this user id does not exists then it will come under this block 
  // Create User   
End
Else
Begin
  // User Already Exists.
End

So the sql query we used in above example is,

=>  Select 1 from Users where UserEmail= 'jatin@dotnetaddas.com'

what it will do is, it will check for the email we passed, if exits it will give o/p as '1' else no row will displayed.

The Same way we can make use of only 'Exits' keyword, without not, as per project requirement.

Also we can use this "Exits" and "Not Exists" SQL keywords in Where condition as below,

1) While Delete Operation, 

DELETE From Users
WHERE EXISTS 
(SELECT * FROM User WHERE UserEmail = 'jatin@dotnetaddas.com')

2) While Insert Operation,

INSERT INTO Employee
(Emp_id, Emp_name)
SELECT User_Id, User_name
FROM Users
WHERE EXISTS (SELECT *
              FROM Users
              WHERE WHERE UserEmail = 'jatin@dotnetaddas.com');

 3) While Update Statement,

Update Employee
Set Emp_Name = 
         ( SELECT User_Name FROM Users WHERE WHERE UserEmail = 'jatin@dotnetaddas.com') 
WHERE NOT EXISTS (SELECT *
              FROM Users
              WHERE WHERE UserEmail = 'Jay@dotnetaddas.com');




Friday, October 30, 2015

Visual Studio 2012 Not opening

Visual Studio 2012 Not opening

If visual studio not opening, please follow the below steps,

Go to Run -> Type “Regedit”



Run -> Regedit
Run command

Registry window will open,

Registry window

3) Microsoft -> Visual studio


4) Delete 11.0 and 11.0_config folders.

 Note:  After this changes pls. restart the system.

Friday, September 11, 2015

Case Statement in SQL Server

According to msdn : Evaluates a list of conditions and returns one of multiple possible result expressions.

The SQL Server (Transact-SQL) CASE statement is similer to IF-THEN-ELSE statement. You can use the CASE statement within a SQL statement.

Syntax:

 case 
         When expression1 then result1

         When expression2 then result2

          else result
 end







Wednesday, August 26, 2015

How to set a default value for an existing column

How to set a default value for an existing column



Syntax: 
ALTER TABLE [TableName] ADD CONSTRAINT [ContraintName] DEFAULT 'Value' FOR [ColumnName];
Example:
ALTER TABLE EmpMaster ADD CONSTRAINT Test DEFAULT 'Pune' FOR City;

alter column with default value in sql server

Alter column with default value in sql server Or 

Alter column with default constraint in sql server

Syntax: 
ALTER TABLE {TABLENAME} 
ADD {COLUMNNAME} {TYPE} {NULL| NOT NULL} 
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
[WITH VALUES]
Example:
ALTER TABLE EmployeeMaster
ADD City NOT NULL
CONSTRAINT EmpMaster_City DEFAULT 'Pune'

Tuesday, August 25, 2015

how to get xml node value in sql server 2008

How to get XML node value in SQL server 2008


If we have an xml data into sql server and want to retrieve xml Node value as below,

XML Data into SQL Server Column as below,

<Employee>
         <EmployeeNumber>834401</EmployeeNumber>
         <Department>Insurance</Department>
</Employee>

Retrieve value as below,

SELECT 
[xmlColumn].value('(/Employee//EmployeeNumber/node())[1]','nvarchar(max)')as EmployeeNumber,

[xmlColumn].value('(/Employee//Department/node())[1]', 'nvarchar(max)') 
as Department
 
FROM [TableName]

Here [xmlColumn] = “Name of xml column of table”


And [TableName] = “Name of table”