October 19, 2007

What are delegates?

Let me discuss function pointers first to make delegate a bit easier to understand.

Example of function pointer :-

int (SampleClass::*pt2function)(float) = NULL;

/* sample class for example*/
class SampleClass{
public: int DoWork(float argument){ return arguement; }
};

pt2function= &SampleClass::DoWork; //assigning function to pointer

SampleClass objInstance; int result3 = (objInstance.*pt2function)(12);

In above code pt2function is a pointer which can hold an address of a function of class SampleClass and which has same definition as specified in pt2function declaration.

Now delegates are for exactly same purpose as function pointer, only difference is we don't declare any pointers here. Instead .net has implemented it in a completely different way.

Delegates are replacements of function pointers in .Net. Delegate is a kind of class, which mimics declaration of a method that is it supports a return type and arguments Or you can say a named method signature is a delegate.


Syntax (C#) :-

delegate returntype delegate-name(arguements,...n);

Syntax (VB.Net) :-
Delegate [SubFunction] delegate-name(arguements,...n) [as returntype]

Example (VB.Net) :-

Public Delegate Function Sample(ByVal strArguement As String) As String


Example (C#) :-

delegate void Sample(String strArguement);


As we see here delegate support return type and can have arguments also.

Now lets try to pass a function to delegate. I will use the same delegates that i have provided in example above.

Example VB.Net

Public Class Class1
'declaring an object of Sample delegate
Dim t As Sample = New Sample(AddressOf CoolFunction)

Public Function CoolFunction(ByVal t As String) As String
Return t
End Function

public sub CallDelegate()
t("Raj Kiran Singh") 'or t.Invoke("Raj Kiran Singh")
End sub

End Class


Example C#

public class Class1
{
//declaring an object of Sample delegate
Sample t = new Sample(CoolFunction);

public string CoolFunction(string t)
{
return t;
}

public void CallDelegate()
{
t("Raj Kiran Singh");
//or t.Invoke("Raj Kiran Singh")
}

}

So its pretty clear from above code that we declare variables of a delegate just like any other class and we pass address of the function as an argument to the constructor.Then we can call the delegate like a function, as done in CallDelegate procedure/function.

In this post I have said that delegate are kind of class. Well, here is some arguement to support my statement. When you declare a delegate, using keyword "delegate", .net compiler treats this syntax in special way. It will create a class with same name as delegate, this class inherits System.MultiCastDelegate class. Although this will be completely hidden from the developer and is performed by the compiler internally.


So for this code -
delegate void Sample(String strArguement);

Compiler will generate a class named Sample. Sample class will have one constructor and three functions. BeginInvoke, EndInvoke and Invoke.

Delegate Class Generated by .Net Sample class extends System.MulticastDelegate class. All delegates are derived from this class. Delegate class has got a private variable which stores an integer that CLR uses to identify method which is to be called back. Delegate class also has a variable which holds the object to be referenced if the method pointer points to an instance method, in case of static or shared functions this variable is null.

When delegate has got more than one method attached to it, its called delegate chains. In this case delegate will execute each method and return the value of last method called. Delegate class has got a private variable to store the preceding delegate. This way it forms a list of delegates.

Labels: , , , ,

October 17, 2007

SQL Server Definitions Part - 2

What is difference between delete and truncate command?

  • Delete commands removes rows from a table. Delete physically removes rows one at a time and records each deleted row in the transaction log. Truncate table command removes all rows from a table without logging the individual row deletes.
  • Truncate table command runs faster than delete command.
  • Truncate command will reinitialize identity columns of a table where as delete command will preserve the identity count. Since
  • Truncate command is not logged it will not and cannot activate a trigger. Truncate command cannot be used with tables referenced by foreign keys.

What do you mean by DBCC statements?
DBCC stands for Database Console Command statements. These statements are used to check the phsyical and logical consistency of database. Following are the categories of DBCC statementss -

  • Status statements
  • Miscellaneous statements
  • Maintenance statements
  • Validation statements

Labels: ,

Difference between a Value Type and a Reference Type in .Net

DotNet Framework is an Object Oriented programming platform. Memory allocation for data members of a program in .net is divided in two types Value type and References type. As the name suggests Value types store data directly in them where as reference types keep a reference of the data. Every object created in .net is derived from System.Object class whether its value type or a reference type.

Now, this being a fact how can we differentiate between these two type and what was the need of two different types. In order to understand these we need to look in to where DotNet creates variables. DotNet uses two data structures Stack and Heap to store data members. Every declaration whether its a value type or a reference type is stored in stack.

int _i;
System.Data.DataSet objSet;

Above declarations are stored in stack, but the integer variable will also stores its value on stack and the DataSet variable will keep a reference of dataset obj. Actual Data Set object is created in heap. Value types are generally small in size so they are kept directly on stack, whereas reference can be huge so they managed differently, this gives performance advantage to compiler.

Also each value type will be derived from System.ValueType which again is derived from System.Object.

All basic data types like Integers, Long, Float, Decimal, Char and Double etc are value types in .Net, beside these every structure and enumeration is a value type.

How to determine whether an object is value type or reference type?

Code Sample -

Dim objset as new System.Data.DataSet();
if objset.GetType().IsValueType then
MessageBox.Show("Value Types")
else
MessageBox.Show("Reference Types")
End If

Labels:

Widening Conversion and Narrowing Conversion

What is Widening Conversion and Narrowing Conversion?
Allowable implicit conversion if the destination type can accommodate all possible value from the source type is called Widening Conversion. For Example -

Dim _i as Integer = 1
Dim _d as Double = 1.002
_d = _i

If the range or precision of the source type exceeds that of the destination type, the operation is called narrowing conversion, which usually requires explicit conversion.

Labels: , ,

October 16, 2007

Some SQL Server Definitions

Most of the definitions provided here are taken from SQL Server 2000 books and Documentation.

What is normalization?
A relational database is basically composed of tables that contain related data. So the Process of organizing this data into tables is actually referred to as normalization.

What is a Stored Procedure?
It is a set of T-SQL statements combined to perform a single task of several tasks and stored in compiled state in SQL server.

What is a trigger?
Triggers are basically used to implement business rules. Triggers is also similar to stored procedures. The difference is that it can be activated when data is added or edited or deleted from a table in a database.

What is a view?
A view can be thought of as either a virtual table or a stored query. The data accessible through a view is not stored in the database as a distinct object. What is stored in the database is a SELECT statement. The result set of the SELECT statement forms the virtual table returned by the view. A user can use this virtual table by referencing the view name in Transact-SQL statements the same way a table is referenced. (Source - SQL Server books online)

What is an Index?
An index is a structure that orders the values of one or more columns in a database table, for example the last name (lname) column of the employee table. If you were looking for a specific employee by his or her last name, the index would help you get that information faster than if you had to search all the rows in the table.The index provides pointers to the data values stored in specified columns of the table, and then orders those pointers according to the sort order you specify.

What is a Clustered Index?
An index in which the logical order of the key values determines the physical order of the corresponding rows in a table.

What is a Non-Clustered Index?
An index in which the logical order of the index is different than the physical, stored order of the rows on disk.

What are Cursors?
Set of rows returned by a SELECT statement is called result-set. Cursors are an extension provided to result-set.
Cursors extend result processing by:
- Allowing positioning at specific rows of the result set.

- Retrieving one row or block of rows from the current position in the result set.

- Supporting data modifications to the rows at the current position in the result set.

- Supporting different levels of visibility to changes made by other users to the database data that is presented in the result set.

- Providing Transact-SQL statements in scripts, stored procedures, and triggers access to the data in a result set.

Whats is HAVING Clause and Where it is used?
HAVING clause specifies a search condition for a group or an aggregate. HAVING can be used only with the SELECT statement. It is usually used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.

Labels: ,