Wednesday, June 29, 2011

Saturday, June 25, 2011

Why object of abstract class can’t be instantiate

We all knows that object of abstract class can be created but cannot be instantiated, but important question is why its not allowed to instantiate the object of abstract class?

Lets understand it with an example:
 public abstract  class cal
    {
        public cal()
        {

        }
        public cal(int a)
        {

        }
        public abstract int add(int a, int b);

    }

public class c2 
    {
            cal c;// Yes we can create object of abstract class
            c = new cal() // error, we can't instantiate object of abstract class.

       
    }

as we can see in above example that we cannot instantiate the object of an abstract class, this is because if it allows to instantiate the object of abstract class and if you will call its function named "add" (abstract) then what will happen? as there is no function body/functionality is defined for that "add" method, as its functionality will be defined in its child class so CLI not allows us to instantiate the object of abstract class.

An abstract class has a protected constructor (by default) allowing derived types to initialize it.

An abstract type is defined largely as one that can't be created. You can create subtypes of it, but not of that type itself. The CLI will not let you do this.
Read More

Friday, June 24, 2011

Can we create clustered index on non primary key column

Yes we can create clustered index on non-primary key column but that column must be unique and the primary key column of that table must have non-clustered index.
By default primary key column contains clustered index so its recommended to create such non-primary key clustered index column first and then should create primary key column so in such case the primary key on that column will be with non-clustered.
But its highly recommended to create primary key column as a clustered indexed column.

Compiled By: Rajesh Rolen

Read More

What is use of parameterized constructor in abstract class? As Abstract classes cannot be instantiated



public abstract class ABC
{

int _a;

  public ABC(int a)
  {
    _a = a;

   }

public abstract void computeA();

};
The above question is little bit tricky, it wants to make you confuse by emphasizing on keyword “Abstract”, where as it has got nothing to do with abstract class.
First of all its essential to understand the constructor call chain. Whenever an object of a class created, its constructor gets called and that constructor calls its parent class’s constructor then this calling chain continuous till super most class’s constructor get called, then first the super most class’s constructor gets executed first then its child class’s constructor (of same chain) and at last the constructor of class whose object has been created will execute.
In the above example scenario where we have only parameterized constructor and no default “Non Parameterized” constructor, the child/inherited class will have to call constructor of its parent abstract class and will have to pass parameters in it.
Like:
public class Foo : ABC
{
    // Always pass 123 to the base class constructor
    public Foo() : base(123)
    {
    }
}
So you don't necessarily need to pass any information to the derived class constructor, but the derived class constructor must pass information to the base class constructor, if that only exposes a parameterized constructor. Also the child class have to override all abstract functions of its parent class.

Compiled By: Rajesh Rolen

Read More

Thursday, June 16, 2011

What is difference between Private and Sealed Classes



Sealed Class:

"Sealed" keyword provide us facility to prevent a class to be inherited by other classes, so "Sealed" classes cannot be inherited by any other class.

You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.

"Sealed" class can have constructor, and they can only be used by its object (non-static methods).

Private Class:

Private class cannot be used directly under any namespace, means we can only create private class as a nested class, to make it available only to class to whom its nested.

the only/mostly use of Private class/structure is to create a user-defined type, which you wants to be accessible to that class only.

NOTE:Nested/inner class can be public also and that can be accessible to inherited class or by object where as private nested/inner class is only accessible to its outer class .


Compiled By: Rajesh Rolen

Read More

Tuesday, June 14, 2011

Override style of css class for some elements in page

I had to change the image of handle of jquery's slider control for some specific page, i have done that by below style, which i created in the page in which i had to change the image of slider's handel.

< style type="text/css" >
    .ui-slider-horizontal .ui-state-default
    {
        background: white url(../images/sliderhandle.png) no-repeat scroll 50% 50%;
    }
    .ui-slider-horizontal .ui-slider-handle
    {
        top: 0.9em;
        border: 0px;
    }
< /style >


Compiled By: Rajesh Rolen

Read More

Thursday, June 9, 2011

Cast Int to Enum and vice-versa


Many of times we face a situation where we are to cast integer/string value to Enum or vice-versa


Cast Int To Enum

From a string:
YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);

From an int:
YourEnum foo = (YourEnum)yourInt;

From number you can also:
YourEnum foo = Enum.ToObject(typeof(YourEnum) , yourInt);


Cast Enum To Int

int something = (int)Question.Role;

Get Enum type as a string: YourEnumName.ItsOption.ToString()

Compiled By Rajesh Rolen

Read More

Monday, June 6, 2011

Magic Tables in SQL Server


Magic Tables

There are two Magic tables available in SQL Server named "Inserted" and "Deleted".
These are mantained by SQL server for Internal processing whenever an update, insert of delete occur on a table.
so These are not physical tables, only Internal tables

When ever insert statement is fired the "Inserted" table is populated with newly inserted Row and
when ever delete statement is fired the "Deleted" table is populated with the deleted row.
in same way when update statement is fired both "Inserted" and "Deleted" table used for records, the Original row before updation get store in "Deleted" table and new row (Updated) get store in "Inserted" table.

The term “magic table” is nowhere in the official product documentation. “Magic tables” is apparently .NETspeak for “inserted and deleted tables”

- SQL Server creates and manages them.
- They live in memory.
- These tables are temporary, and only accessible from the statement that created them.

This Magic table are used In SQL Server 6.5, 7.0 & 2000 versions with Triggers only.

But, In SQL Server 2005, 2008 & 2008 R2 Versions can use these Magic tables with Triggers and Non-Triggers also.

Using with Triggers:
If you have implemented any trigger for any Tables then,
1.Whenever you Insert a record on that table, That record will be there on INSERTED Magic table.
2.Whenever you Update the record on that table, That existing record will be there on DELETED Magic table and modified New data with be there in INSERTED Magic table.
3.Whenever you Delete the record on that table, That record will be there on DELETED Magic table Only.

These magic table are used inside the Triggers for tracking the data transaction.

Using Non-Triggers:
You can also use the Magic tables with Non-Trigger activities using OUTPUT Clause in SQL Server 2005, 2008 & 2008 R2 versions.

For PHP Bros Please refer : http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html

Compiled By Rajesh Rolen

Read More

Thursday, June 2, 2011

Get Ranking using ROW_Number() without specific ordering in SQL Server


As we all knows that, to get ranking with query we use ROW_Number() in sql server but this issue with Row_Number() is that it only works with "order by" and sometimes we face a situation where we wants the ranking without doing any change in its ordering. To do so, you can do a trick to provide order by to Row_Number as well as it will not affect ordering of your result.

With ordering, this will provide result with row_number but in asc ordering by field1:
 select field1, field2,ROW_NUMBER () OVER (ORDER BY field1) AS RowNum from TableName

Without ordering, this will provide result in order as of query with row number:
 select field1, field2,ROW_NUMBER () OVER (ORDER BY (SELECT 1)) AS RowNum from TableName
so we have used "(select 1)" instead of specifying any field name for ordering so it will not affect order of query result

Compiled By: Rajesh Rolen

Read More

Wednesday, June 1, 2011

Multi record Insert into column with unique key constraint and ignore duplicate

Lets say i have got a table named tblcity and it contains two columns cityid and cityname.
this table contains lets say 1000 records.
now wants to insert some cities from a another table into this table and i wants that if city already exists in tblcity then it should not be reinserted.

to do so, mysql provides very good facility to ignore duplicate like:

INSERT IGNORE INTO Table2(Id, Name) SELECT Id, Name FROM Table1

Source: My Dear Friend Jitendra Dhoot

unfortunately sql server not provide this facility to use "ignore" keyword to ignore duplicate values while performing bulk insert.

To do so in sql server you will have do:
When you are creating a unique index on column, at that time you can set it to "ignore duplicates", in which case SQL Server will ignore any attempts to add a duplicate.

Otherwise we have 3 other option:

Using NOT EXISTS:

INSERT INTO TABLE_2
  (id, name)
SELECT t1.id,
       t1.name
  FROM TABLE_1 t1
 WHERE NOT EXISTS(SELECT id
                    FROM TABLE_2 t2
                   WHERE t2.id = t1.id)
Using NOT IN:

INSERT INTO TABLE_2
  (id, name)
SELECT t1.id,
       t1.name
  FROM TABLE_1 t1
 WHERE t1.id NOT IN (SELECT id
                       FROM TABLE_2)
Using LEFT JOIN/IS NULL:

INSERT INTO TABLE_2
  (id, name)
   SELECT t1.id,
          t1.name
     FROM TABLE_1 t1
LEFT JOIN TABLE_2 t2 ON t2.id = t1.id
    WHERE t2.id IS NULL


Compiled By Rajesh Rolen

Read More
Powered By Blogger · Designed By Seo Blogger Templates