Thursday, August 12, 2010
What is a Delegate?
What is a Delegate?
delegates are function pointers that point to function of matching signatures.On the other hand .NET framework has introduced a type-safe mechanism called
delegates, with automatic verification of the signature by the compiler.While using
delegates it is very much necessary to make sure that the functions which the delegates points has the same number of argument type and same return type. For example if we have a method that takes a single string as a parameter and another method that takes two string parameters, then we need to have two separate delegate type for each method.Types of Delegate
Delegates are of two types:
Single cast delegate
Adelegate is called single cast delegate if it invokes a single method. In other words we can say that SingleCast Delegates refer to a single method with matching signature.Multicast Delegates
MultiCast Delegates are nothing but a single delegate that can invoke multiple methods of matching signature. MultiCast Delegate derives from System.MulticastDelegate class which is a subclass of System.Delegate.
For example if we are required to call two methods on a single button click event or mouse over event then using MultiCast Delegates we can easily call the methods.
Friday, August 6, 2010
How to Detect an Error in T-SQL
More precisely, if SQL Server emits a message with a severity of 11 or higher,
@@error will hold the number of that message.
And if SQL Server emits a message with a severity level of 10 or lower,
SQL Server does not set @@error,
and thus you cannot tell from
There is no way to prevent SQL Server from raising error messages.
There is a small set of conditions for which you can use SET commands to control whether these conditions are errors or not
@@error is set after each statement.
Example
CREATE TABLE notnull(a int NOT NULL)The output is:
DECLARE @err int,
@value int
INSERT notnull VALUES (@value)
SELECT @err = @@error
IF @err <> 0
PRINT '@err is ' + ltrim(str(@err)) + '.'
Server: Msg 515, Level 16, State 2, Line 3
Cannot insert the value NULL into column 'a', table
'tempdb.dbo.notnull'; column does not allow nulls. INSERT fails.
The statement has been terminated.
@err is 515.
Wednesday, August 4, 2010
How can we take decision about when we have to use Interface and when Abstract Class
How can we take decision about when to use Interface and when Abstract Class.
When we thinking about the entity there are two things one is intention and one is implemntation.
I am trying to make a Content Management System where content is a genralize form of article, reviews, blogs etc.
| CONTENT |
| ARTICLE |
| BLOGS |
| REVIEW |

So content is our base class now how we make a decision whether content class should be Abstract class, Interface or normal class.
First normal class vs other type (abstract and interface).
If content is not a core entity of my application means as per the business logic if content is nothing in my application only Article, Blogs, Review are the core part of business logic then content class should not be a normal class because I’ll never make instance of that class.
So if you will never make instance of base class then Abstract class and Interface are the more appropriate choice.
Second between Interface and Abstract Class.
| CONTENT Publish () |
| ARTICLE |
| BLOGS |
| REVIEW |

As you can see content having behavior named “Publish”. If according to my business logic Publish having some default behavior which apply to all I’ll prefer content class as an Abstract class.
If there is no default behavior for the “Publish” and every drive class makes their own implementation then there is no need to implement “Publish” behavior in the base case I’ll prefer Interface.
These are the in general idea of taking decision between abstract class, interface and normal class. But there is one catch.
As we all know there is one constant in software that is “CHANGE”.
If I made content class as Interface then it is difficult to make changes in base class because if I add new method or property in content interface then I have to implement new method in every drive class.
These problems will over come if you are using abstract class for content class and new method is not an abstract type.
So we can replace interface with abstract class except multiple inheritance.
CAN-DO and IS-A relationship is also define the deference between Interface and abstract class.
Interface can be use for multiple inheritance
for example we have another interface named “ICopy” which having behavior copy and every drive class have to implements its own implementation of Copy.
If “Article” class drive from abstract class Content as well as ICopy then article “CAN-DO” copy also.
IS-A is for “generalization” and “specialization” means content is a generalize form of Article, Blogs, Review and Article, Blogs, Review are a specialize form of Content.
So, abstract class defines core identity.
If we are thinking in term of speed then
abstract is fast than interface because interface requires extra in-direction.
Abstract Class vs Interface -1
Abstract Class vs Interface
We can not make instance of Abstract Class as well as Interface.
Here are few differences in Abstract class and Interface as per the definition.
Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).
Interface can only contain abstract methods, properties but we don’t need to put abstract and public keyword. All the methods and properties defined in Interface are by default public and abstract.
//Abstarct Class
public abstract class Vehicles
{
private int noOfWheel;
private string color;
public abstract string Engine
{
get;
set;
}
public abstract void Accelerator();
}
//Interface
public interface Vehicles
{
string Engine
{
get;
set;
}
void Accelerator();
}
abstract class contains private members,
we can put some methods with implementation also.
But in case of interface only methods and properties allowed.